Closed (won't fix)
Project:
Drupal core
Version:
5.0-beta1
Component:
javascript
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
4 Nov 2006 at 12:12 UTC
Updated:
14 Nov 2006 at 20:16 UTC
i'm using the following code in my module
drupal_add_js(url(drupal_get_path('module', 'mymodule'). '/js/myscript.js'), 'module', 'header', FALSE, TRUE);
and this results in a wrong path:
/drupal-5.0-beta1//drupal-5.0-beta1/index.php?q=modules/mymodule/js/myscript.js
please fix this.
Comments
Comment #1
Rok Žlender commentedI believe you are not providing right parameters to drupal_add_js function. First parameter should be path to your .js file relative to base_path read http://api.drupal.org/api/HEAD/function/drupal_add_js .
What you are doing is:
1.) drupal_get_path returns
modules/mymodule2.) you append /js/myscript.js now you have
modules/mymodule/js/myscript.js3.) url function creates url to this file
/drupal-5.0-beta1/index.php?q=modules/mymodule/js/myscript.js4.) this step is done by drupal. When drupal builds html code for display it prepands every js file path with base_path() and thats why you get
/drupal-5.0-beta1//drupal-5.0-beta1/index.php?q=modules/mymodule/js/myscript.jsWhat you need to do is just call
I think this is not a bug report but I don't know if I should mark it as fixed.
Comment #2
Tobias Maier commentedComment #3
hass commentedSorry, this is partly wrong. if i remove url() i get the URL
/drupal-5.0-beta1/modules/mymodule/js/myscript.jsas you wrote. But this is *not* the menu path i registered with the module. it must be:
/drupal-5.0-beta1/index.php?q=modules/mymodule/js/myscript.jslet's say - this is not a real static JS file. it's a PHP include, sending a special JS MIME type and returns dynamic JS code...
and only if i'm using CleanURLs i get
/drupal-5.0-beta1/modules/mymodule/js/myscript.jswhat is finaly correct, too. but all this requires URL() to work properly and this is critical bug.
Comment #4
hass commentedHEY - this is NOT FIXED!
Comment #5
Crell commentedYou shouldn't be adding JS paths to a dynamically generated script in the first place. The system is not, AFAIK, built for it. If you're doing something where you need to do that, then it means you're going about it the wrong way. Instead, use drupal_add_js() as intended and write Javascript code that can take parameters. Those parameters you can dynamically make available to it in a variety of ways, including hidden form variables, inserting a script block into the head directly that just sets some variables, etc.
Dynamically generating JS application code in PHP, as opposed to just JS declarations to pass to a function, is a sure sign that you need a more generalized JS function, not a dynamically built one.
Comment #6
hass commentedThis one has worked in Drupal 4.7.x very well - why is this changed for 5.0 and not on the list of changes? This must be a bug!
Asside i think you haven't understand what i'm doing.
1. i created a module.
2. this module requires to add some JS code to every page
3. this module is a frontend to the JS file.
4. this module saves settings required in the JS file in the drupal database
5. every theme have different settings and therefor the JS is dynamic
6. if someone requests the path
/drupal-5.0-beta1/index.php?q=modules/mymodule/js/myscript.jshe gets a dynamicaly build JS file with special variables declared for this special theme and page and the correct mime type.Please, tell my how this can be done in a different way. And let me say some other modules are using this variant, too:
Example:
so, this ends up the URL() function is doing something wrong or
drupal_get_path('module','mymodule')makes no sense.Regards
Alex
Comment #7
Crell commentedThe problem is that you're trying to dynamically build a Javascript function via PHP. Don't. That's the Wrong Way of doing it, Drupal or otherwise. In Drupal's case in particular, that means that every page request is going to generate *two* complete runs of the entire Drupal system, once for the page and once for the Javascript. Even if it worked, the performance hit is terrible.
The right way to have variable javascript is to have a static JS file that gets loaded once, and then uses some globally defined variables to decide what to do. You can set those variables with drupal_set_html_head(), or even better drupal_add_js() has the ability to set per-page settings.
Comment #8
hass commentedI think i understand about this possible performance issue, but this is not the problem with caching enabled. Asside this DB requests are only for my module variables i must get from variable table. i don't see the real problem and other moduls do the same. You won't tell me it is not allowed to use
drupal_set_headerand some logics behind - isn't it? simply search forheader,drupal_set_headerordrupal_get_headerany you will find a bunch of modules using it for dynamicaly create JS, XML, and so on (examples: devel, gsitemap, urllist, quicktags, etc) and so on. Performance is a reasonable point, but caching should be in place and this is not forgotten.drupal_set_html_head()won't be a good idea, while it is bad SEO practice to add JS code to a pagehead. i know it is technical correct, but for search engines this is somewhat a very big bad idea... however some developers are doing wrong without the SEO's knowledge. It is recomended to move JS code to external JS files for XHTML.drupal_add_js()requires to have static code dupliacted. this is not good and i don't like to do this. Additional i must write a frontend for writing JS files to disk, what is not required and awful... asside of the filesystem permission problems i will get with such a module. if it's in private store i will have a drupal run to get out the file, too. if the file is saved in modules dir everone run in permission troubles.You haven't answered my question about - why it works in Drupal 4.7.x . I reactivated the bug, while this has worked in 4.7 and is now broken! And it should be fixed for compatibility with IIS and Apache, regarding cleanurl's and standard URLs. This is the thing we need the helpfull function
url()for and i'm sure others will run into this problem, too. My dynamic JS files are only a small example...Comment #9
hass commentedi thought more about this issue.
if i have a
iframeinserted to a page - with a module, let say for example for advertisements, and this one calls for a PHP file inside my modules directory, i have the same issue... therefor this is not limited to JS, XML.Comment #10
jessegri commentedI'm having a similar issue. With Clean URL's turned on any page other that the root page the logo goes missing. I checked the URL's to the logo files and found that it was appending the node name for the page I was on was getting appended to the URL for the logo, see the example below.
On the Root page
http://example.org/drupal-5beta/files/color/garland-ab55ed1d/logo.png
On the blog page
http://example.org/drupal-5beta/blog/files/color/garland-ab55ed1d/logo.png
Setting a base URL in settings.php did not override this issue. Looks like Clean URL's may need some further inspection and bug squishing. Naturally turning off Clean URL's fixes this problem.
Comment #11
Rok Žlender commented@jessegri
There is an issue http://drupal.org/node/92696 which solved the missing logo problem. This patch is not included in beta1. Please try to reproduce the error with Drupal HEAD which is the most up to date (unstable) version of Drupal. You can get it here http://ftp.osuosl.org/pub/drupal/files/projects/drupal-cvs.tar.gz
Comment #12
jessegri commented@Rok Žlender
Thanks, that fixed my problem beautifully. Sorry for posting in the wrong area for this issue.
Comment #13
Steven commentedhass: please stop re-opening this issue. Several people have said that dynamically generating a JS file is not the proper way to go about this. At the very least, it is not supported by drupal_add_js(). No amount of stomping your feet is going to change that.
As for 'it worked in 4.7', this was only a side effect. drupal_add_js() unconditionally prepended base_path() to the path to the JavaScript file. So, if you did not use Clean URLs or were running Drupal in a subdirectory, you would not be able to generate the correct path using any combination of drupal_add_js() or url().
What Crell and others are suggesting is not to add your entire JS code to every page, but simply to use the 'setting' mode of drupal_add_js() to add only those variables that change to the HTML header. All of this is documented.
The actual code would still be in a static, external, cacheable JS file, and you get a nice separation of data and code. For example, color.module outputs a chunk like this:
But all the other code is in modules/color/color.js.
As far as PHP includes and iframes, this has nothing to do with it. Those would be paths to real files, so you never want to prefix with "?q=" at all. url() should not be used in this case. It exists to make links to 1) Drupal menu paths 2) External URLs.
Comment #14
hass commentedif you think url() is working correctly, please give me a hint how to solve the problems i described.