Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get content inside script as text

I would like to print the content of a script tag is that possible with jquery?

index.html

<script type="text/javascript">      function sendRequest(uri, handler)     {       } </script> 

Code

alert($("script")[0].???); 

result

function sendRequest(uri, handler) {   } 
like image 655
DVD Avatar asked Feb 17 '12 19:02

DVD


People also ask

How can I get script tag content?

You can use native Javascript to do this! External tag can be loaded dynamically instead of inline. Then the code will be inserted between opening and closing tags. It works for me. is there some way to use this method to select script tags that have a specific type attribute?

Can we write HTML inside script?

Adding JavaScript into an HTML DocumentYou can add JavaScript code in an HTML document by employing the dedicated HTML tag <script> that wraps around JavaScript code. The <script> tag can be placed in the <head> section of your HTML or in the <body> section, depending on when you want the JavaScript to load.

Can I put script inside body?

Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.

Can we have script inside div?

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


1 Answers

Just give your script tag an id:

<div></div> <script id='script' type='text/javascript'>     $('div').html($('#script').html()); </script> ​ 

http://jsfiddle.net/UBw44/

like image 136
Vigrond Avatar answered Sep 29 '22 11:09

Vigrond