4 replies [Last post]
swoodard
Offline
Regular
San Francisco, CA
Last seen: 12 years 47 weeks ago
San Francisco, CA
Joined: 2004-11-19
Posts: 44
Points: 0

I ain't gonna lie, I'm a javascript super-begginner, but I have a question. How do you use an anchor to activate a javascript on another page. What I mean is, you would use ' href="page.html#anchor" ' to get to a page anchor, but can you put an anchor in one page that will not only switch the browser to another, but activate a script when it gets there, like ' href=page.html#javascript:fn( arg1, arg2) '. How would you go about doing this.

Thanks,

Sam

Tags:
BonRouge
BonRouge's picture
Offline
Enthusiast
Sendai, Japan
Last seen: 11 years 46 weeks ago
Sendai, Japan
Timezone: GMT+9
Joined: 2005-07-10
Posts: 237
Points: 0

A javascript question

I could be wrong, but I think you'd need to use something like php. Then you could use $_GET to get the variables from the url and print a 'window.onload' event on to the page to make it do what you want to do. That's what I'd do, though as I say, there might be a better way.

swoodard
Offline
Regular
San Francisco, CA
Last seen: 12 years 47 weeks ago
San Francisco, CA
Joined: 2004-11-19
Posts: 44
Points: 0

A javascript question

simlarly, is there a $_SET?
Sam

Chris..S
Chris..S's picture
Offline
Moderator
Last seen: 7 years 46 weeks ago
Timezone: GMT+1
Joined: 2005-02-22
Posts: 6078
Points: 173

A javascript question

To have a script activate after the page has finished loading you need to attach it to the documents onload script. The simplest method is:

<body onload="myscript();">

Where the function myscript() is defined within <script> tags or an included js file somewhere else.

You can access the url used to access the page via a couple of methods.
- location.href
- document.URL

I don't know if the url returned will include the fragment identifier (that's the #someid) and the query string (that's the ?key1=value1&key2=value2 bit) in all browsers. FF and IE certainly do.

With those two bits of knowledge you should be able to extract the fragment identifier (or query string) value from the URL and have your script take whatever action it needs.

I am not certain what events fire in response to a "goto #fragment". Its possible the element may receive focus if its capable of receiving focus. In which case you could attach something to an onfocus event. Then again, maybe that's not how things work.

Anonymous
Anonymous's picture
Guru

A javascript question

swoodard wrote:
simlarly, is there a $_SET?
Uh, what's that supposed to do? :?

http://us3.php.net/variables.predefined