Forum Moderators: open

Message Too Old, No Replies

Display Javascript code from JQuery

         

jojy

4:16 pm on Mar 24, 2009 (gmt 0)

10+ Year Member



Hello,
I have an external javascript file which I want to call using json/jquery. It works fine except jquery clears everything in the page and display that javascript output. Here is the example function which is called after successful completion of form submission.


function Show(result) {
if(result.success){
//$('#div').html(result.msg);

$('#div').html('<script language='javascript' type="text/javascript" src="path/to/js">'); //clears everything in the page and just display the js output

//this is what I js is printing
$('#div').html('<script language='javascript' type="text/javascript">document.write("hello")</script>');
}
}

The problem seems in jquery, any help would be much appreciated.

Thanks

Fotiman

5:42 pm on Mar 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The problem is not jQuery, the problem is document.write. If you call document.write after the page has finished loading, it will replace your existing document with whatever you write. Perhaps try using DOM methods instead and appending to your document.

jojy

1:14 am on Mar 25, 2009 (gmt 0)

10+ Year Member



Thanks Fotiman for your reply, I am trying to display score to users using javascript and they have option to embed it in their sites/blog. I was wondering how can I append html elements? I have to create parent element before I use it and to achieve this I have to use document.write. Any alternative?

daveVk

2:27 am on Mar 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



perhaps

//this is what I js is printing
$('#div').html('hello');
}

jojy

10:56 am on Mar 25, 2009 (gmt 0)

10+ Year Member



this does not create "div" element on the page.

daveVk

12:18 pm on Mar 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Then maybe

$("<div>hello</div>").appendTo("body")