Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

An error in browser

Status
Not open for further replies.

multichild

Programmer
Jan 28, 2006
76
GB
I have an error in my JScript, which I have found, but dont know how to fix.

Here is the error:

'scagain' is null or not an object

Here is the code:

var strbrw=navigator.userAgent.toLowerCase();if(strbrw.indexOf("msie") > -1 && strbrw.indexOf("mac")<0){var flashnagain;scagain=document.getElementById("flashnobj");scagain.outerHTML = scagain.outerHTML;}function unflashn(){if (strbrw.indexOf("msie") > -1 && strbrw.indexOf("mac") < 0){if (document.getElementsByTagName){var flashnobjs = document.getElementsByTagName("OBJECT");for (i=0; i<flashnobjs.length; i++){flashnobjs.outerHTML = "";}}}}window.onunload=unflashn;

Can anybody help

lee

Accend Web Solutions

 
[tt]<script language="javascript" type="text/javascript" [blue]defer="defer"[/blue]>
//stuff those lines here
</script>
[/tt]
 
Hi,

I did that:

<script language="javascript" type="text/javascript" defer="defer">
var strbrw=navigator.userAgent.toLowerCase();if(strbrw.indexOf("msie") > -1 && strbrw.indexOf("mac")<0){var flashnagain;scagain=document.getElementById("flashnobj");scagain.outerHTML = scagain.outerHTML;}function unflashn(){if (strbrw.indexOf("msie") > -1 && strbrw.indexOf("mac") < 0){if (document.getElementsByTagName){var flashnobjs = document.getElementsByTagName("OBJECT");for (i=0; i<flashnobjs.length; i++){flashnobjs.outerHTML = "";}}}}window.onunload=unflashn;
</script>

Now i got a syntax error

lee

Accend Web Solutions

 
This script isnt on the web page but pulled in as a jscript file, through the link below, so will this influence the help you just gave me.

<script src="News_Scroller/flashnjs.js" type="text/javascript"></script>

lee

Accend Web Solutions

 
I would start looking at the rest of the script where errors were not surfaced due to the above mishap took the early blame.
 
Just to make things easier to read, use code tags
Code:
 and
and break your code into separate lines with indentation. My eyes are seeing line noise...
 
Code:
var strbrw=navigator.userAgent.toLowerCase();if(strbrw.indexOf("msie") > -1 && strbrw.indexOf("mac")<0){var flashnagain;scagain=document.getElementById("flashnobj");scagain.outerHTML = scagain.outerHTML;}function unflashn(){if (strbrw.indexOf("msie") > -1 && strbrw.indexOf("mac") < 0){if (document.getElementsByTagName){var flashnobjs = document.getElementsByTagName("OBJECT");for (i=0; i<flashnobjs.length; i++){flashnobjs[i].outerHTML = "";}}}}window.onunload=unflashn;

The code is one lenth of it. The error is so annoying though. I just cant work it out

Accend Web Solutions

 
Do you know how to use indentation to make your code readable by humans? It would help you AND us if you did that.

Lee
 
I am starting out at this kind of thing, the boss at Accend gave me this to do, and to answer your question using his forumn name, No!

But I would be gratefull if you could show me.

Accend Web Solutions

 
Ok, here goes!

Step one: break code into separate lines. ; is usually the end of a statement. The only time it's not the end of a statement is in "for" statement. { and } define "blocks" of code. Give them their own line at this step.

Code:
var strbrw=navigator.userAgent.toLowerCase();
if(strbrw.indexOf("msie") > -1 && strbrw.indexOf("mac")<0)
{
var flashnagain;
scagain=document.getElementById("flashnobj");
scagain.outerHTML = scagain.outerHTML;
}
function unflashn()
{
if (strbrw.indexOf("msie") > -1 && strbrw.indexOf("mac") < 0)
{
if (document.getElementsByTagName)
{
var flashnobjs = document.getElementsByTagName("OBJECT");
for (i=0; i<flashnobjs.length; i++)
{
flashnobjs[i].outerHTML = "";
}
}
}
}
window.onunload=unflashn;

Still hard to sort out...

Step 2: Indent! Remember that { and } determine blocks of code? Indent what's inside them with a couple spaces or a tab.
Code:
var strbrw=navigator.userAgent.toLowerCase();
if(strbrw.indexOf("msie") > -1 && strbrw.indexOf("mac")<0)
{
  var flashnagain;
  scagain=document.getElementById("flashnobj");
  scagain.outerHTML = scagain.outerHTML;
}

function unflashn()
{
  if (strbrw.indexOf("msie") > -1 && strbrw.indexOf("mac") < 0)
  {
    if (document.getElementsByTagName)
    {
      var flashnobjs = document.getElementsByTagName("OBJECT");
      for (i=0; i<flashnobjs.length; i++)
      {
        flashnobjs[i].outerHTML = "";
      }
    }
  }
}

window.onunload=unflashn;

Now you can see what's going on. You can also open the JavaScript console and it will give you errors about what line the error is on. This is now meaningful information, since there's one statement on each line, not 20!

There's some controversy about whether the { and } should be indented with the block of code they contain, but that's an internal matter for your organization, which obviously doesn't have coding standards. Just pick one style (align with statement above or align with block) and stick with it!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top