/    Sign up×
Community /Pin to ProfileBookmark

onload="function();"

hi all,

i’m trying to get a function in a js file to run when a html page is loaded

the js file is called customise.js

in the header of the html i have

<script type=”javascript” src=”customise.js”></script>

and to call it i do

<body onload=”function();”>

but nothing happens – what am i missing?

Thanks
Alex

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@KorJul 24.2006 — You may call a function, not a file. That function must have a certain name, not simply: function().

for instance

function myFunction(){

.......

}

Now, in order to separate the content, it is better to call in inside the javascript code, not from the tag body

<script type="text/javascript">

function myFunction(){

.......

}

onload=myFunction;

</script>
Copy linkTweet thisAlerts:
@alzzzauthorJul 24.2006 — thanks for your reply

the function isn't called function - i just did that for presentation!

Surely a function defined in a javascript file can be called?

if not - is there a way around this as the function is reusable in different webpages and i don't want to have to rewrite it every time

thanks,

Alex
Copy linkTweet thisAlerts:
@KorJul 24.2006 — Of course you may use an external js file, there is no need to repeat the whole code in every page! If your function is not called onload, there must be another error, we could not know which, unless you provide us the code or an attachment or a link towards.
Copy linkTweet thisAlerts:
@alzzzauthorJul 24.2006 — apologies

in html file

<script type="javascript" src="customise.js"></script>

<body onload="customise(document);">

in js file

function customise()

{

Alert("here");

var keywordArray = new Array();
var keywordSelenium = new Array();

//create command
keywordArray[keywordArray.length + 1] = "create";

var bodyhtml = document.getElementById("body");
var savehtml = bodyhtml.innerHTML;

..................

document.body.innerHTML= savehtml;


}


thanks again
Copy linkTweet thisAlerts:
@KorJul 24.2006 — [COLOR=Red]a[/color]lert("here");

and probably

keywordArray[[COLOR=Red]keywordArray.length[/COLOR]] = "create";

as long as there is no use to create the [1] indexed element without creating the [0] one.
Copy linkTweet thisAlerts:
@alzzzauthorJul 24.2006 — thanks for spotting them - (silly errors by me!)

actually just found the reason why it wasn't being called

<script type="javascript" src="customise.js"></script>

should have been

<script type="[B]text/[/B]javascript" src="customise.js"></script>

thanks very much kor
Copy linkTweet thisAlerts:
@KorJul 24.2006 — take care for the future,,, a lot of silly errors come out from little mistypes. ?
×

Success!

Help @alzzz 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.20,
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,
)...