Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

JavaScript/jQuery: Insert < and >

Posted on 2011-02-11
3
Medium Priority
?
438 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:Gurvinder Pal Singh
Gurvinder Pal Singh earned 200 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 400 total points
ID: 34872961


//Try this instead:

$(function() {

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

});

0
 
LVL 83

Accepted Solution

by:
leakim971 earned 1400 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Suggested Courses

886 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