Zachrip
Zachrip

Reputation: 3382

How can I change a variable of my javascript, then run the "onload" without reloading the page

I have a site that picks a new song every day and just uses xml docs and stuff..that doesn't matter, what matters is I would like to test tomorrows song, by changing a variable, then "emulating" the onload, so that it changes the content to tomorrows page. Is there any way to do the onload, without reloading the page(which changes my variable back)?

Upvotes: 1

Views: 157

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270657

You should be able to trigger it from your browser's console. Open the console (or Firebug) and allow the page to load normally. Then set the new variable in the console. Just call window.onload() (or document.body.onload() if that's how you defined it) and your function should fire with the new value.

// In the browser console:
yourVariable = 'tomorrowSong.mp3';
// Updated: 
// Since you have bound body.onload, use:
document.body.onload();
// Or call the function directly in the console:
onpageload();

It appears your variable is defined in scope of your function, which makes it difficult to change from the console. Instead, define the variable outside the function. That way, you can redefine it simply in the console

// Define v at a higher scope
// Outside the function...
var v; 

Upvotes: 3

Related Questions