Forum Moderators: open

Message Too Old, No Replies

Call external js file

conditionally

         

Birdman

2:01 pm on Nov 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello,

I am trying to get an external file to load and display when a condition is met.

What happens is, the page is a list of events that is dynamically generated server-side. There is a jump-link pointing to each event from the previous page(eg. /events_10-2003.html#12345). I need the external script to load directly under the event listing that the link jumps to.

Now, I have already found out that there is no way to get the value of the anchor (#12345) server-side, so I went to js.

Here is what I tried:


Head:
str = window.location.hash;
str = str.substr(1);

Body(this script appears after every listing):
if (str == <?=$db->f("id")?>) {
document.write("<script type=\"text/javascript\" src="ht*p://www.site.com/script.js\"></script>");

It works as planned in my Opera browser but IE shows the js in the wrong place.

If anyone can shed some light, I would appreciate it.

Birdman

Sinner_G

2:05 pm on Nov 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don't know if that's the problem, but you are missing a \ just after src=.

Birdman

2:19 pm on Nov 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello Sinner_G,

Sorry, it's a typo. It's correct in the actual file. It just looks like IE doesn't want to display it in the right place.

If I just call the file from the html like this:

<script type="text/javascript" src="..."></script>

It appears in the right position but I have no way of knowing when to call it server side bacause I can't get the hash(anchor) value until it hits the client.

Birdman

2:21 pm on Nov 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Adding the value as a querystring will give me the value to work with server side but then it makes all the urls appear to be different pages(from SE standpoint).

What a pain!

Sinner_G

2:49 pm on Nov 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Very strange. Have you tried on other browsers (Netscape,...)?

And where is the JS displayed in IE?

Birdman

2:54 pm on Nov 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I haven't tried any other browsers except Opera and IE. In IE, the js is displayed above the fold. It almost looks like it is in the position of the screen where it should be, just displayed before the jump.

Did that make sense?

Birdman

2:57 pm on Nov 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What I mean is, if you go to the top of the page and mark the position on the screen where the js is and then scroll down to exactly where the jump link would have put you, the previous mark is exactly where the js should be.

That didn't make sense either, did it?

Sinner_G

3:06 pm on Nov 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did not make much sense, but I'm used to not making sense myself, so I think I got it more or less. :)

Just a wild guess, but have you tried putting the if function in the head of your page and calling it from the HTML body with something like checkanchor()?

RonPK

3:50 pm on Nov 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just some thoughts, not sure whether they are any help:

there is no way to get the value of the anchor (#12345) server-side

Do you mean this in a general sense? PHP f.i. has a function parse_url() which returns an array with a part called 'fragment', containing stuff after the hashmark #. Even without such a function, it can't be that hard to parse the documents location?

if (str == <?=$db->f("id")?> )

Maybe you need to quote the string:
if (str == '<?=$db->f("id")?>')

document.write("<script [...]></script>");

For valid HTML, you should escape the / in </script>. Otherwise some over-correct SGML parser might consider </ as the closing tag of your script.

Hope this helps...