-1

I have this in the head of my document:

<script type="text/javascript">

            var myString= location.href;
            var mySplit = myString.split("#");
            var x = mySplit[1];

            if (x == 'page1_div1') {
                document.getElementById('div1').className = 'theNewClass';
            }
</script>

What is my error? Thanks, Linda

5
  • What is the problem? Is it not running? Are the results not what you expect? Do you get an error message? Does your computer smoke? Commented Jan 23, 2010 at 6:10
  • Wouldn't the JavaScript error popup tell you what the error is? Commented Jan 23, 2010 at 6:10
  • >What is the problem? Is it not running? Are the results not what you expect? I expect that the class of the div would change and it isn't changing. If it is 'theOldClass' and the URL has the hash of 'page1_div1' then I want to change the class of the div to 'theNewClass' . I assumed that I had a syntax error since I am a newbie. Commented Jan 23, 2010 at 6:15
  • Try debugging the javascript using Firebug in Firefox Commented Jan 23, 2010 at 6:21
  • Does this answer your question? Executing JavaScript in the <head>, getElementById returns null Commented Jun 22, 2024 at 5:23

2 Answers 2

3

If you have that code in the head of your document as you say, the problem is that document.getElementById is not able to find your "div1" element, because when it is executed, the body of your document hasn't been evaluated, try to use the window.onload event:

window.onload = function () {
  var x = location.hash.substring(1);

  if (x == 'page1_div1') {
    document.getElementById('div1').className = 'theNewClass';
  }
};

Note that I also simplified your code a little bit, you wanted to extract the hash part of the current url.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. Thank you very much especially for the explanation for why it was not finding div1.
0

You can get the hash more easily through location.hash and since you will likely have several divs, it's easier to switch on them. Example:

<script type="text/javascript">
  var myHash = location.hash || 'NO-HASH';

  switch (myHash) {
    case 'NO-HASH':
      // Original page - no hash.
      document.getElementById('div1').className = 'theOldClass';
      document.getElementById('div2').className = 'theOldClass';
      break;
    case '#page1_div1':
      document.getElementById('div1').className = 'theNewClass';
      document.getElementById('div2').className = 'theOldClass';
      break;
    case '#page1_div2':
      document.getElementById('div1').className = 'theOldClass';
      document.getElementById('div2').className = 'theNewClass';
      break;
  }
</script>

However, if this is related to your history tracking question, you'll want to run this code in a polling timer.

1 Comment

>if this is related to your history tracking question, you'll want to run this code in a polling timer. It is a similar problem but not the same. The first question was between div's on the same page. This question was about navigation between different pages. On the link to the first page, I am putting a hash that does not resolve on the linked page just to have some tracking between pages. Max, how to I go about hiring you to solve my javascript problems :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.