Reputation: 169
I am trying to perform a document.write() to display a dynamic new.php HREF for each of the 3 conditions below (#pane1, #pane2, #pane3).
Currently I can get the URL to display but only with the single quotes included (which doesn't write the correct value, it just displays/writes the text as one would expect). If I take out the quotes around the newURL variable I get "ReferenceError: newURL is not defined" and the entire Add New text does not display as desired.
<script type="text/javascript">
$(function(){
var newURL="";
// Bind the event.
$(window).hashchange( function(){
if(location.hash=="#pane1") {
alert( location.hash );
newURL="new1.php";
}
if(location.hash=="#pane2") {
alert( location.hash );
newURL="new2.php";
}
if(location.hash=="#pane3") {
alert( location.hash );
newURL="new3.php";
}
})
// Trigger the event (useful on page load).
$(window).hashchange();
});
</script>
<li>
<script type="text/javascript">
document.write('Add New'.link('newURL'));
</script>
</li>
Upvotes: 1
Views: 82
Reputation: 10216
the document write
could be executed before the document ready event triggers, also that variable is in the document ready function scope. try putting the var declaration outside of the jquery scope:
var newURL="";
$(function(){
...
and then at the bottom:
<script type="text/javascript">
document.write('Add New'.link(newURL));
</script>
or also wrap this in a document ready function to make sure this line gets executed after you triggered the hashchange event on the window object.
Upvotes: 2