/    Sign up×
Community /Pin to ProfileBookmark

Putting the onload in the script

Hello, how can I put an onload in the script itself, instead of in the body. So that this <BODY onload=”link();”>
goes in the following script instead of the body and hooked up to the id “url.”

[QUOTE]

<script type=”text/javascript”>
arr = new Array(
[“Link name google”,”http://www.google.com“],
[“Link name yahoo”,”http://www.yahoo.com“],
[“Link name msn”,”http://www.msn.com“]// no comma at the end
);
function link(){
document.getElementById(“url”).href = arr[new Date().getSeconds()][1];

document.getElementById(“url”).innerHTML = arr[new Date().getSeconds()][0]; setTimeout(‘link()’,1000);}
</script>
This in the body
<a href=”#” id=”url”></a>

[/QUOTE]

Reason why is because I am going to be using more than one onload later. I’ts going to be easier to have my onloads hooked up to the id. Please let me know, thank you very much

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@NedalsApr 24.2007 — If you want a number of onloads...

function init() {

link();

etc

}

window.onload = init; // no ()
Copy linkTweet thisAlerts:
@vortexerauthorApr 24.2007 — I have this but the code no longer works. It says arr1[] is null or not an object now. Can anyone see whats wrong? Thanks.
<script type="text/javascript">

arr1 = new Array(

["Link name google","http://www.google.com"],

["Link name yahoo","http://www.yahoo.com"],

["Link name msn","http://www.msn.com"]// no comma at the end

);

arr2 = new Array(

["Link name google","http://www.google.com"],

["Link name yahoo","http://www.yahoo.com"],

["Link name msn","http://www.msn.com"]// no comma at the end

);

function link(){

document.getElementById("url1").href = arr1[new Date().getSeconds()][1];

document.getElementById("url1").innerHTML = arr1[new Date().getSeconds()][0]; setTimeout('link()',1000);}

function link2(){

document.getElementById("url2").href = arr2[new Date().getSeconds()][1];

document.getElementById("url2").innerHTML = arr2[new Date().getSeconds()][0]; setTimeout('link()',1000);}

function init() {

link();link2();

}

</script>

</head>

<body>

<script type="text/javascript">

window.onload=init;</script><a href="#" id="url1"></a><a href="#" id="url2"></a>

</body>

</html>[/QUOTE]
Copy linkTweet thisAlerts:
@FangApr 24.2007 — [I]new Date().getSeconds()[/I] returns an integer between 0 and 59. Your array is too small.

You are also referencing a 2 dimensional array, but your's are 1 dimensional.
Copy linkTweet thisAlerts:
@vortexerauthorApr 24.2007 — Doh, I get what your saying I shortened it to test. What do you mean about the dimensions, can't spot it?
Copy linkTweet thisAlerts:
@jaffamolApr 24.2007 — His arrays are 2 dimentional but the problem is how you said about it returning a nmber between 0-59 but the arrays are only 0-2.

I think I know what your trying to do, are you trying to create a script that changes the link every second?
Copy linkTweet thisAlerts:
@ricpApr 24.2007 — You are doing this..

MyArray = new Array( [item 1a,item 1b], [item 2a,item 2b], ... )

The problem with that is that the [] signifies a literal array, so you end up having this..

MyArray = new Array( new Array(item 1a, item1b), new Array(item 2a, item2b), ... )

Thus creating a multidimensional array.

Personally I would do it like this..


MyArray = [

{ name: "Google", url: "http://www.google.com" },

{ name: "Yahoo", url: "http://www.yahoo.co.uk" }

]

That way you can query: [I]MyArray[0].name[/I] and [I]MyArray[0].url[/I]
Copy linkTweet thisAlerts:
@FangApr 24.2007 — Oops! missed the comma in the array :o

This would be better:function link(){
var seed=Math.floor(Math.random() * arr1.length);
document.getElementById("url1").href = arr1[seed][1];
document.getElementById("url1").innerHTML = arr1[seed][0]; setTimeout('link()',1000);}
Copy linkTweet thisAlerts:
@vortexerauthorApr 25.2007 — Thanks everybody, I'll try them.
×

Success!

Help @vortexer spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...