Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unexpected call to method or property access.

 
amit sanghai
Ranch Hand
Posts: 231
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to append a <script> tag to a div at runtime. The script has a function. But I am getting the following error on IE:

Unexpected call to method or property access.

It works in Firefox. Can anyone please help me.

var script = document.createElement("script") ;
script.setAttribute("language", "javascript") ;

var functionText = "function test(){alert('hello')}" ;
script.appendChild(document.createTextNode(functionText)) ; // this statement gives the error.


Thanks and regards,
AmIt Sanghai.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Eric
[ July 30, 2007: Message edited by: Eric Pascarello ]
 
amit sanghai
Ranch Hand
Posts: 231
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, but I want to add the function at runtime using DOM not directly as static code.

var script = document.createElement("script") ;
script.setAttribute("language", "javascript") ;
script.appendChild(document.createTextNode(function)) ;
 
Pat Flickner
Ranch Hand
Posts: 173
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit, is there a reason why you can't create the items as Eric shows you? Are the values being passed that require you to create code on the fly? If so, you need to be clear. Even so, you can get the information from the request if that's being passed and then load that information into your document via a script within the portion of the body where the item needs to be. Please provide more information.

If you are doing this to add a level of complexity to your code to make you appear more valuable or knowledgeable, please don't. Complexity is the hallmark of the junior programmer. If you can do it simply, then do it simply. Save complexity for real problems.
 
amit sanghai
Ranch Hand
Posts: 231
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to create the code on the fly. This is the need of the project. I am getting all the drop down picklist data from xml and on change of its value I need to call other dependant picklists which are hidden at first but become visible later. So the javascript code is also dependant on the drop downs.
 
amit sanghai
Ranch Hand
Posts: 231
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the answer. I have to dealy the calling of the onChange event handler by writing an anonymous function and calling it on onchange.

var select = document.createElement("select") ;
var functionArguments = xmlNode.getAttribute("functionArguments") ;
var functionText = xmlNode.getAttribute("onChangeFunctionText") ;

select.onchange = new Function(functionArguments, functionText) ;


Thanks.
 
Thamizh Arasu
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi amit sanghai,

I have also the same problem. i want the solution. can you share your solution with me.
[ August 04, 2007: Message edited by: Bear Bibeault ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic