Solved

JavaScript/jQuery: Insert < and >

Posted on 2011-02-11
3
426 Views
Last Modified: 2012-08-14
How can I append < and > using JavaScript?

This uses entities so the JavaScript is not executed.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>Demo</title> 
<link href="css/screen.css" rel="stylesheet" type="text/css" /> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
</head> 
<body> 
 
<h1>Hello</h1> 

<a href="#" onclick="xyz(); return false;">Click here to run a dynamically created function</a>

<script type="text/javascript"> 
$('body').append('&lt;script type="text/javascript"&gt;function xyz(){alert(12345);}&lt;/script&gt;');

</script> 

</body> 
</html>

Open in new window

0
Comment
Question by:hankknight
3 Comments
 
LVL 40

Assisted Solution

by:gurvinder372
gurvinder372 earned 50 total points
ID: 34872956
why are you not simply using?

$(document).ready(function(){
$('body').append('<script type="text/javascript">function xyz(){alert(12345);}</script>');
});
0
 
LVL 20

Assisted Solution

by:Proculopsis
Proculopsis earned 100 total points
ID: 34872961


//Try this instead:

$(function() {

  $('body').append('<' + 'script type="text/javascript">function xyz(){alert(12345);}<' + '/script>');

});

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 350 total points
ID: 34873000
Use :
<script type="text/javascript">
	var myScript = document.createElement("script");
	myScript.language = "javascript"; // you may remove this line
	myScript.type = "text/javascript"; // you may remove this line
	myScript.text = "function xyz(){alert(12345);}";
	document.getElementsByTagName("body")[0].appendChild(myScript);
</script>

Open in new window

Test page :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>Demo</title> 
<link href="css/screen.css" rel="stylesheet" type="text/css" /> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
</head> 
<body> 
<h1>Hello</h1> 
<script type="text/javascript">
	var myScript = document.createElement("script");
	myScript.language = "javascript";
	myScript.type = "text/javascript";
	myScript.text = "function xyz(){alert(12345);}";
	document.getElementsByTagName("body")[0].appendChild(myScript);
</script>
<a href="#" onclick="xyz(); return false;">Click here to run a dynamically created function</a>
</body> 
</html>

Open in new window

0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question