Solved

JavaScript/jQuery: Insert < and >

Posted on 2011-02-11
3
423 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
This article discusses how to create an extensible mechanism for linked drop downs.
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)

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now