最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - "SCRIPT5002 function expected" error in IE - Stack Overflow

matteradmin5PV0评论

i am facing an issue and getting error like "SCRIPT5002 function expected" in internet explorer 7-9. this is my code :

 var myDiv = document.getElementById("divId"); //this line gives me "SCRIPT5002 function expected" error.

 myDiv.style.cssText("position:absolute;z-index:999");
myDiv.appendChild(
        JavaScriptCode);

so how to solve this??

i am facing an issue and getting error like "SCRIPT5002 function expected" in internet explorer 7-9. this is my code :

 var myDiv = document.getElementById("divId"); //this line gives me "SCRIPT5002 function expected" error.

 myDiv.style.cssText("position:absolute;z-index:999");
myDiv.appendChild(
        JavaScriptCode);

so how to solve this??

Share Improve this question asked Feb 5, 2013 at 8:41 user1918096user1918096 1531 gold badge3 silver badges10 bronze badges 2
  • 1 what are you trying to acplish? especially with the "appendChild" part? - I don't think the error is in the first line. – OschtärEi Commented Feb 5, 2013 at 8:43
  • How is that script embedded in your page? Is it the only script? – Bergi Commented Feb 5, 2013 at 8:47
Add a ment  | 

2 Answers 2

Reset to default 2

The problem should be in the 2nd line:

myDiv.style.cssText("position:absolute;z-index:999");

cssText is not a function, but a property. So call it like this:

myDiv.style.cssText = "position:absolute;z-index:999";

or (better approach in my opinion, because it is clearer):

myDiv.style.position = 'absolute';
myDiv.style.zIndex = 999;

I also got this in an attempt to check if a variable was an Element.

"notAnElement" instanceof Element

And it always throws the function expected error.

document.createElement("div") instanceof Element

Successfully evaluates to true

I haven't implemented it yet, but my solution is to use a try/catch block.

Post a comment

comment list (0)

  1. No comments so far