Solved

JavaScript/jQuery: Insert < and >

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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…

756 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