最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论