Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run script tags loaded with ajax

General Question

Say you have a simple ajax request like $("#container").load('/url #content');. The loaded DOM will be something like

<a>email</a>
<script>/* some script */</script>

What can I do to get the contents of the script tags to execute?

Specific Question

Cloudflare automatically protects email addresses in the DOM by replacing the text with [email protected] and including a script that unravels it. However, the contents can be loaded asynchronously and the script that does the unraveling won't be executed. I could just turn off the email protection feature (I think) but I'm wondering if there's another way around this.

Example

See it in action at http://aysites.com/what -- click "Contact"

like image 529
Explosion Pills Avatar asked Jul 11 '12 04:07

Explosion Pills


People also ask

How do I run a script tag?

The “script” tag JavaScript programs can be inserted almost anywhere into an HTML document using the <script> tag. You can run the example by clicking the “Play” button in the right-top corner of the box above. The <script> tag contains JavaScript code which is automatically executed when the browser processes the tag.

Can I use JavaScript in AJAX?

AJAX is not a programming language. AJAX just uses a combination of: A browser built-in XMLHttpRequest object (to request data from a web server) JavaScript and HTML DOM (to display or use the data)

Can I add script tag inside div?

You can add <script></script> inside a DIV tag. Just check on w3c, it is valid HTML.

How does AJAX call work in JavaScript?

How AJAX Calls Work. AJAX uses both a browser built-in XMLHttpRequest object to get data from the web server and JavaScript and HTML DOM to display that content to the user. Despite the name “AJAX” these calls can also transport data as plain text or JSON instead of XML.


1 Answers

There is a pure js function which invokes the JavaScript compiler. It's eval. You need to use it.

Examples:

jQuery.globalEval("var newVar = true;")

on jQuery.com

Or

eval("x=10;y=20;document.write(x*y)");
document.write("<br />" + eval("2+2"));
document.write("<br />" + eval(x+17));

on w3schools

like image 118
Tooraj Jam Avatar answered Nov 15 '22 08:11

Tooraj Jam