how to insert javascript code via jquery?

<html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script> </head> <body> <div id="example"> </div> <script type="text/javascript"> function insert() { var data = '<script type="text/javascript"> function test() { a = 5; }<\/script>'; $("#example").append(data); } function get() { var content = $("#example").html(); alert(content); } </script> <a href="#" onclick="insert();">INSERT</a> <a href="#" onclick="get();">GET</a> </body> </html>

what i want to do:

when i click on insert, i want to insert this code into example div:

<script type="text/javascript"> function test() { a = 5; }<\/script>

when i click on get, i want to get that code, which i inserted there.

but when i click on insert, and then on get, there's no code.. where is problem ? thanks

-------------Problems Reply------------

According to your comment to Adam Bellaire, you want the script tag to display as normal text. What you are looking to do is encode the text with HTML entities, this will prevent the browser from processing it as normal HTML.

var enc = $('<div/>').text('<script type="text/javascript"> function test() { a = 5; }<\/script>').html();
$("#example").append(enc);

This works:

function insert() {
var data = '<script type="text/javascript"> function test() { a = 5; }<\/script>';
$('#example').text(data);
}

function get() {
var content = $('#example').text();
alert(content);
}

Are you checking for the code by browsing the live version of the DOM, using a tool like Firebug? If you are expecting to see your code rendered in your regular browser window, you won't, because the script tags are actually parsed when they are inserted, and script tags aren't visible elements in an HTML page.

Category:javascript Views:0 Time:2009-09-16

Related post

  • Is there a way to convert this javascript code to jquery? 2011-04-16

    Is there a way to convert this javascript code to jQuery? document.writeln('Nu te teme, căci Eu sunt cu tine; nu te uita cu îngrijorare, căci Eu sunt Dumnezeul tău; Eu te întăresc, tot Eu îţi vin în ajutor. Eu te sprijin cu dreapta Mea biruitoare.”')

  • Change javascript code to jquery code, How is it? 2012-04-06

    I want change following javascript code to jquery code, How is it? With done change hourOffset in date 3, 21 and 9, 22. DEMO: http://jsfiddle.net/zJRDC/ <script type="text/javascript"> var interval = self.setInterval("clock()", 1000); function

  • Using jQuery to insert Javascript code into a DIV 2012-04-11

    Okay, so I'm working on a responsive site and I'm trying to handle Adsense in the best way possible that doesn't get me banned! What I'd like to do is use jQuery to add in my Adsense code only if the browser width is less than 533px. This way, I can

  • Insert javascript code in a list form. 2011-07-28

    I have a list, and want to insert custom javascript code in a new form for this list (when form loaded). For example, when new form for this list is opened, I want to make some layout modifications for this form. How it can be done? And how many ways

  • cant use javascript code with jquery 2010-12-09

    I have used the script at: http://www.javascript-coder.com/ i use the following jquery code in which i am calling its methods. $("form[name='registeruser'] input[name='register']").live('click',function(){ //validateregister($("form[name='registeruse

  • How can I insert html code to Jquery Dialog Container in Zend framework Jquery Form 2011-06-06

    I'm currently working Zend framework and placing Zendx_Jquery_Form in Jquery Dialog Container Just wanted to know is there a way to insert html code (so i can put a picture in there) to the Jquery Dialog Container in zend framework. This is my code t

  • Insert javascript code into conference paper 2011-08-27

    Can anyone reccommend the best way to put javascript code into a conference type paper. The paper is times new roman, size 10. In Microsoft Word 07 Ive tried Insert > Object > Object... > OpenDocument Text..But this method leaves the code to

  • How to insert Javascript code in the body instead of Head in GreaseMonkey 2011-10-06

    I need a javascript function to load first before calling it with GreaseMonkey. but it seems that GreaseMonkey always puts my code in the head of the html. How can I insert my code in the body instead. Preferably at the very end --------------Solutio

  • Weird Javascript code around jQuery plugins 2011-11-26

    Possible Duplicate: What is the purpose of wrapping whole Javascript files in anonymous functions like “(function(){ … })()”? I have seen this kind of code specially when dealing with jQuery plugins. Can someone please explain me what this does? (fun

  • Insert JavaScript code from JavaScript 2012-04-27

    i need gets a html code by jsonp like method, and display this on a div element. This works with simple html like an images, text, etc. But now the code can content a JavaScript tags and need insert this on a div, but the javascript, don't runs, i re

  • client JavaScript and/or JavaScript code using jquery invoking WCF Service retrieving a collection of System.IO.Stream? 2012-05-01

    I am creating a WCF service that will be consumed by plain JavaScript on the client side and some jQuery JavaScript on the client side as well. 1) How can I implement the plain client JavaScript so that it will invoke the WCF Service in such a way th

  • JavaScript and/or JavaScript code using jquery invoking WCF Service retrieving a collection of System.IO.Stream? 2012-05-02

    I am creating a WCF service that will be consumed by plain JavaScript on the client side and some jQuery JavaScript on the client side as well. 1) How can I implement the plain client JavaScript so that it will invoke the WCF Service in such a way th

  • How can I convert normal javascript code to jQuery 2012-01-17

    People are recommending me that I should convert this code to jquery, but how and where, are there any tools or how do you do that and why XMLHttpRequest is not good with jQuery?? I have this code: function showVal(str) { if (str == "") { document.ge

  • How to add Javascript code using jQuery and setTimeout() 2010-07-13

    I have some tracking code that the provider (WebTraxs) says should be placed at the bottom of the tag. The problem is that this same code is causing everything on the page (including my jQuery code) to run AFTER the WebTraxs is executed. This executi

  • how do i insert javascript code into colorbox? 2011-06-09

    I need to execute a javascript into a colorbox modal window. I've heard about placing an onComplete callback but i've no idea on how to write it...i'm totally noobie to jquery/javascript. Can anyone explain me step by step ? Thanks --------------Solu

  • How to insert Javascript code in a Ruby injection 2011-09-01

    I have 2 text fields. One is for the input of a member's id, and the other is the output of the member's name. When I type a member's id in the first field and the action onblur is triggered, I would like to show the member's name in the other field.

  • Generifying javascript code for jQuery and knockout.js to be a reusable module 2012-04-26

    I am trying to build a very specific module that integrates Twitter bootstrap (for the tabs), jQuery (for ease of development) and knockout.js for client-side view models. The idea is that I have one HTML page, which has 3 div's defined in it. Each d

  • How can I convert this working JAVASCRIPT code to JQUERY 2009-05-22

    <script language="javascript" type="text/javascript"> function hasPasswordChanged(value) { if(value == '1') { var container = document.getElementById("sNav"); if(document.getElementsByTagName) { var hyperLinkList = container.getElementsByTagNam

  • How can I performance tunning on that javascript code? (JQuery selector based) 2012-03-23

    How can I performance tuning in that code var isHighLighted = this.HighlightCell(this.GetOutcomeInList(i), i, "selectedByEditor "+((isCommonChoice) ? "Common" : "") ); if (!isHighLighted) { isHighLighted = this.HighlightCell(this.GetOutcomeInOtherBet

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.169 (s). 11 q(s)