Question: I am writing javascript to my web pages, but there is a number of functions and loops, that i think are running in all pages, so the first one is running and failing on the second page. Because of this, the javascript function on the second page is not running.

Can anyone give me an idea of how to create page-specific functions or check the availability of an id? I don't use any frameworks.

thanks in advance.

my javascript code is :

```
window.onload = function(){  
   var yellows = document.getElementById('magazine-brief').getElementsByTagName('h2');  
   var signUp = document.getElementById('signup-link');      
    function animeYellowBar(num){  
      setTimeout(function(){  
          yellows[num].style.left = "0";  

          if(num == yellows.length-1){  
              setTimeout(function(){  
                  signUp.style.webkitTransform = "scale(1)";  
              },num * 250);  
          }  
      }, num * 500);  
    }
    for (var i = 0; i < yellows.length; i++){  
      animeYellowBar(i);  
    }

    alert("alert second page");  
}

```

in this code, the alert message not working on second page. any idea?

Answer: 
The last statement creates a temporary stringstream and then uses it to parse the string as hexadecimal format into iMask.

There are flaws with it though, as there is no way to check that the streaming succeeded, and the last stream achieves nothing as you are dealing with a temporary.

Better would be to create the stringstream as a non-temporary, ideally using istringstream as you are only using it to parse string to int, and then checking whether the conversion succeeds.

```
std::istringstream iss( strMask );
iss >> std::hex;
if(!( iss >> iMask ))
{
 // handle the error
}

```

You only need to set the mode back to decimal if your stringstream is now about to parse a decimal integer. If it is going to parse more hex ones you can just read those in too, eg if you have a bunch of them from a file.

How you handle errors is up to you.

`std::hex` and `std::dec` are part of the `<iomanip>` part of streams that indicate the way text should be formatted. hex means "hexadecimal" and dec means "decimal". The default is to use decimal for integers and hexadecimal for pointers. For reasons unknown to me there is no such thing as a hex representation for printing float or double, i.e. no "hexadecimal point" although C99 sort-of supports it.
The code takes the string `optarg` and, treating it as hex, converts it to an integer and stores it in iMask.

If you remove the std::hex modifier you can parse the input as decimal. However, I usually use boost's lexical\_cast for this. For example:

```
int iMask = boost::lexical_cast< int >( strMask );

```