Geting contents of javascript loaded in browser - javascript

How do I get the contents of a loaded script at runtime without using ajax to read the script from the source?

if the source of the script is written inside the script tag, like so:
<script>
source comes here
</script>
then you can use the innerHTML attribute to retrieve it, but if the script is loaded with the "src" attribute then I think that you can not use the dom to get the content.
i believe that your only option is to use the src attribute, and then issue an ajax request to get the source.
check this: How can I get the content of the file specified as the 'src' of a <script> tag?

I think if you actually want the text of the loaded script you need to request it with ajax rather than a script tag. That way you can inspect the response as text and eval it if you need to. If using jQuery you can use jQuery.getScript.
I'm still not entirely sure why you need to access the source, it would be cleaner to form the script you are loading as an object with fields you can query from the second script, rather than reading the source.

Related

Find out redirected location of Javascript source

My HTML page has a <script> tag, which embeds a Javascript source file indirectly through a PHP script, e.g.
<script src="http://my.server.com/myjs.php?version=1"></script>
Based on the parameters to myjs.php (version only being an example) the PHP file simply sets the location
header("Location: version/1/mynewfile.js");
and the browser then automatically loads mynewfile.js.
Unfortunately mynuewfile.js needs to know the URL where it is loaded from, e.g. http://my.server.com/version/1/mynewfile.js.
Without using redirection, the globally executed code in mynewfile.js simply inspects the src property of the first element. Unfortunately, this only returns http://my.server.com/myjs.php?version=1. How can mynewjsfile.js get the real (redirected) URI of the script?
Of course, the PHP file could "simply" read the JS file and set a global variable to the JS file, but redirection is a really elegant solution and should be kept if possible.

Is it possible to get the text of a script that is loaded from a source as opposed to in line code?

Currently I'm just taking source of a script that is on the page.
The HTML:
<script type="text/plain">meow</script>
The JavaScript:
// returns "meow"
document.querySelector('script').text
I want to be able to load the script from another file.
The HTML:
<script type="text/plain" src="file.txt"></script>
file.txt:
meow
The JavaScript:
// returns "meow"
document.querySelector('script').textFromFile
Does anyone know if that's possible? I would assume it's not, and I haven't found anything on google that is what I'm asking.
If the script has a src attribute you would need to fire of a request via XHR to that same path and pull in the text content of its response. Keep in mind this will require additional work if the script is being loaded from another domain. At that point you would need to make use of CORS, or introduce some other type of proxy to handle the cross-domain communication.

Script tag with charset dynamically added to Unicode-header page is not processed properly

In PHP, I have a page whose header is set as unicode. (not through meta-tags. I have PHP setting the charset to UTF-8.)
After the page is displayed, I need to dynamically add a <script> tag that refers to another domain's code.
While the cross-domain Javascript already referred to in the HTML of the page gets parsed properly, the dynamically-added remote Javascript "Charset" is ignored.
Ergo, two JS files from the same domain get interpreted very differently. The one that was already loaded into the HTML gets processed properly, but the one that's dynamically added is processed incorrectly... Even though I'm supplying the proper charset.
Here's my example code:
$('#footer').after( $('<script>').attr('type','text/javascript').attr('charset','ISO-8859-1').attr('src','//remoteJavascript.js') );
I'm also using jQuery 1.6.2.

Modifying Javascript to run after AJAX load

I am using a little javascript thingy (app?) from http://code.google.com/p/tumblrbadge/ to load my most recent tumblog post into my webpage. However, when I load the 'tumblr' section with AJAX using Jquery, the script does not get executed. I understand why this is and that I need to include the javascript file in the and execute it after the AJAX load is complete. My problem is this: I do not fully understand the tumblrbadge code and, when I include the script in the and call tumblrBadge() after loading, it does not run. How must I modify the tumblrbadge code to allow it to be run on demand from the ?
All of this is hosted at http://jquerytest.webs.com
It looks like your problem is that the script tags within the tumblr section of your site are not being executed. When you insert html into a page using ajax, you have to parse out the contents of any script tags and execute them separately.
After the content of the tumblr tab is inserted in your page, get all of its script tags with $("#contentOfTumblrTab script") and evaluate their innerHTML using eval().
Try $(window).onload instead of $(document).ready.

URL Hash modification after document.write()

I download via jQuery AJAX a whole html webpage. I want to replace the content of the current page with the one downloaded via ajax. I do it with document.write(). It doesn't work correctly because whenever I try to modify the hash, the webpage is reloaded.
I know in IE it it necessary an iframe, but that is not the problem, because I use jQuery History plugin. The problem is due to the use of document.write(), but I don't know why.
Update:
index.php -> main entry point, which downloads JS code to parse URL after hash and invoke request.php.
request.php -> request entry point. It returns the webpage.
It works OK when I simulate a direct request to request.php and the downloaded webpage updates the hash.
It doesn't work (in FFox only) when I simulate a original request to index.php, which downloads the webpage via request.php and the downloaded page modifies the hash.
I use document.write() to write the content of the webpage to the current window. So the problem is about the modification of the hash in a document "being written".
don't use document.write().
instead use $('your selector').html(your_html_fetched_via_ajax);
I thinkg that you can't modify the whole html object because it means erasing the reference to the javascript script tag. I would say your best bet is to either just link to the request.php page or just change the body tag
$('body').html(response_html);
And I agree with harshath.jr, don't use document.write().
The individuals pointing you towards an iframe are correct. Add the iframe, and simply set the src attribute to the page you're fetching...you won't even need request.php.
If you really want to try to load in the html without an iframe, you'd have the parse out the elements in the head and add them to your documents , and also parse the contents of the and add them to the current pages body. Its not guaranteed to display correctly, though. I think an iframe is really what you're looking for.

Categories