Forum Moderators: open

Message Too Old, No Replies

function not loaded

using rewrite rules breaing JS

         

kadnan

4:19 pm on Mar 9, 2008 (gmt 0)

10+ Year Member



i have .htaccess rule which follow particular pattern.

[codes]
RewriteEngine On
RewriteBase /yt
RewriteRule ^listing/([^/]*)/([^/]*) \
listing.html [PT]
[/codes]

the purpose of rule is that if someone access url site.com/listing/foo/bar then it always access listing.html file.

Now it works fine. The problem arose when I tried to call a function on onload() event of listing.html. I am always getting "function blah()" not found. Later when I tried to access url as site.com/listing.html then it fired the event and loaded function "blah".

Now I have to use particular URL pattern anyway, please tell me how do I tackle this JS error?

Thanks

gergoe

4:29 pm on Mar 9, 2008 (gmt 0)

10+ Year Member



If the mentioned function is situated in an external javascript file, and you are referencing to it using relative path names, then you have found the root cause of the problem, the browser can not load the javascript file, so the function you are calling from onload is not declared either.

Use the absolute uri

<script type="text/javascript" src="http://www.example.com/foo.js"/>
, or just the absolute path
<script type="text/javascript" src="/foo.js"/>
when referencing files on your website.

kadnan

4:41 pm on Mar 9, 2008 (gmt 0)

10+ Year Member



this is how I am loading script:

<script src="yt.js"></script>

<body onLoad="playVideo();">

still getting playVideo() not defined. even if I use addEventListener() then still it not firing up.

gergoe

5:01 pm on Mar 9, 2008 (gmt 0)

10+ Year Member



Referencing "yt.js" when the url in the browser is "/listing/foo/bar", would resolve to "/listing/foo/yt.js", and that is the url the browser tries to load the js file from - does not exists, even more, the mod_rewrite directives in your htaccess file will rewrite those request as well to listing.html, so the browser instead of a JavaScript file, will find a html file.

Choose one of the suggestion from my earlier post, that will fix things up, or check out the Apache Web Server [webmasterworld.com] forum, for similar problems (broken images and malfunctioning javascript when rewriting requests).

g1smd

5:09 pm on Mar 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That's your problem right there, as explained one post above.

You need to specify the full path to the script file, in the script tag. That is, the URL will start with a "/" and it will mention any folder structure too.