Reputation: 37
this is my first question here but I hope that I am on point and give all nessecary info. I need to use window.onload to make a page scripted in the head of my html code with type="text/view" to show.
When I put this code in the body and use
<body onload="loadWelcomePage()">
function loadWelcomePage() {
var welcomePageContent = document.getElementById('welcomeview');
var profilePageContent = document.getElementById('profileview');
var showPage = document.getElementById('contentSpace');
showPage.innerHTML = welcomePageContent.innerHTML;
}
However when I copy the same code to a js file the same function won't load. What am I doing wrong. Ill post the replacement code in the js file that I use below.
window.onload = function() {displayView};
function displayView() {
var welcomePageContent = document.getElementById('welcomeview');
var profilePageContent = document.getElementById('profileview');
var showPage = document.getElementById('contentSpace');
showPage.innerHTML = welcomePageContent.innerHTML;
}
I need to use javascript and not jQuery to complete the task.
Upvotes: 2
Views: 1624
Reputation: 943510
function() {displayView};
Just mentioning a variable doesn't do anything.
If you want to call a function, then you have to call it (e.g. with ()
).
function() {displayView(); };
You could also just bypass the creation of a function which does nothing except call another function:
window.onload = displayView;
(Note that the browser will call the function when the event fires, you aren't calling it yourself in this case, so the ()
should be omitted, otherwise you call it immediately and assign the return value to onload
.)
Upvotes: 7