PDA

View Full Version : Javascript/HTML Question - Executing JS when a linked page loads



aaron1
Dec 23rd, 2008, 04:08 AM
Hi,

I need to link to a page and have the following JS be executed when the page opens.

showHide('freshmen');

I'd prefer to do this without a script.. is there a way to do this within an anchor tag, for example if it were on the page itself, I'd just do..

<a href="javascript: showHide('freshmen');">

but it is on a separate page so that complicates things.

Thanks.

abduraooft
Dec 23rd, 2008, 08:42 AM
I need to link to a page and have the following JS be executed when the page opens.

showHide('freshmen');
Adding the following to the head tag would do this.

<script type="text/javascript" >
window.onload=function (){
showHide('freshmen');
}
</script>

I'd prefer to do this without a script.. is there a way to do this within an anchor tag, for example if it were on the page itself, I'd just do.. Sorry, I don't get that.

aaron1
Dec 24th, 2008, 11:54 PM
But wouldn't that just load it from either the page being linked from or the page being linked to regardless of whether the page was accessed via the link..? I do not think you understood my question.

194673
Dec 25th, 2008, 12:28 AM
If you put abduraooft's code only on the targeted page, then it would only affect the linked page. If you are using an external link and opening it in a new window, it gets a bit trickier:



...
<script type="text/javascript">
window.onload = function () {
document.getElementById('link').onclick = function() {
showHide('freshman');
alert(document.getElementById('link').href);
window.open(document.getElementById('link').href,'newWindow');
}
}
</script>
...
<a href="http://google.com" id="link">Some link</a>


Edit: Sorry didn't even test my code. Fixed now.

aaron1
Dec 25th, 2008, 04:55 AM
Well, there will be several different links going to that page, each that has to reveal a different section. Not every link should execute that specific id, can I set the link location and which section id it should execute?

194673
Dec 25th, 2008, 07:20 AM
Try applying an array and a for loop to my previous code.