Solved

JavaScript/jQuery: Insert < and >

Posted on 2011-02-11
3
429 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: 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

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…
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 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

632 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