sumchurch Posted February 10, 2010 Share Posted February 10, 2010 I just learned the power of JavaScript and <div id>! Great! But I have one problem. I am using a JavaScript functions to put text in a box. But I have a .js file from another source that can't be put in my functions. Is there any way to load the file into a variable or object that can then be used in the function?function doit(){a='<script src="http://external.js"></script>';document.getElementById('put_it_here').innerHTML=a;}Doesn't work. Any way to make it happen? Link to comment Share on other sites More sharing options...
justsomeguy Posted February 10, 2010 Share Posted February 10, 2010 This is how you would load an external file: var fileref=document.createElement('script');fileref.setAttribute("type","text/javascript");fileref.setAttribute("src", filename);document.getElementsByTagName("head")[0].appendChild(fileref); There may be some timing issues though, I don't know if that's synchronous or asynchronous. You may have access to the functions in that file as soon as you call the appendChild method, or it may start loading the file at that point and you would have to wait for it to finish loading before having access to the code. Link to comment Share on other sites More sharing options...
sumchurch Posted February 10, 2010 Author Share Posted February 10, 2010 This is how you would load an external file:var fileref=document.createElement('script');fileref.setAttribute("type","text/javascript");fileref.setAttribute("src", filename);document.getElementsByTagName("head")[0].appendChild(fileref); There may be some timing issues though, I don't know if that's synchronous or asynchronous. You may have access to the functions in that file as soon as you call the appendChild method, or it may start loading the file at that point and you would have to wait for it to finish loading before having access to the code. Thanks. I'm doing something wrong.<body><div name="head"></div><script>var fileref=document.createElement('script');fileref.setAttribute("type","text/javascript");fileref.setAttribute("src", "http://www.quotationspage.com/data/mqotd.js");document.getElementsByTagName("head")[0].appendChild(fileref);</script></body> Does fileref become a useable variable? I kind of need a walk-through on this. Link to comment Share on other sites More sharing options...
justsomeguy Posted February 10, 2010 Share Posted February 10, 2010 I'm not sure what you mean by "usable variable", when does a variable become unusable? fileref is a reference to the new script element. That code just creates a new script element, sets the attributes on it, and appends it to the <head> element of the document. Link to comment Share on other sites More sharing options...
jeffman Posted February 10, 2010 Share Posted February 10, 2010 Why can't the external script be linked to the document in the <head> from the beginning? Link to comment Share on other sites More sharing options...
sumchurch Posted February 11, 2010 Author Share Posted February 11, 2010 I'm not sure what you mean by "usable variable", when does a variable become unusable? fileref is a reference to the new script element. That code just creates a new script element, sets the attributes on it, and appends it to the <head> element of the document.I AM that fool who teaches himself! Still can't get my head around OOP and what is going on. (Too many years with BASIC 2.0 and ML on the C-64.)Let me start again. I have a .js from an external source that does something like this:document.write('This is what they send me');I have found that I can put text from a function into a <div id></div> section with document.getElementById(id).innerHTML=STRING;I want to get 'This is what they send me' into STRING, and into the id'ed div area. It should not be all that hard. Right? Link to comment Share on other sites More sharing options...
jeffman Posted February 11, 2010 Share Posted February 11, 2010 It's not hard. Where is STRING? In some external .js file? That should not be a problem as long as the script is embedded correctly and STRING is defined in the global space of that script.One possible issue:document.getElementById(id) only works if the element already exists in the document. That is, if you are getting the reference in the script's global space (the line is being executed while the page downloads) if the lines that create the element have not been read yet, then document.getElementById(id) will not be able to get a reference to the element.You would have to put your call to document.getElementById(id) in an onload function, or simply write it in a script that is physically below the element in the document. Link to comment Share on other sites More sharing options...
justsomeguy Posted February 11, 2010 Share Posted February 11, 2010 document.write('This is what they send me');I have found that I can put text from a function into a <div id></div> section with document.getElementById(id).innerHTML=STRING;I want to get 'This is what they send me' into STRING, and into the id'ed div area. It should not be all that hard. Right?Well, yeah, it sort of is. You're including a Javascript file with a bunch of code in it, but you don't want to execute the code. You want to transform the Javascript into different code and then do something with it. Just attaching a new script element to your page will execute the code, it's not going to let you modify it first. So, it's just going to run all of the document.write statements and that's it. If you want to transform the code to use the text inside the document.write statements in another way, then you'll need to use something like PHP to get the code, transform it, and return it to your Javascript.So, how's your PHP? Link to comment Share on other sites More sharing options...
Synook Posted February 12, 2010 Share Posted February 12, 2010 You could theoretically also use AJAX to load the other JavaScript file as plain text, and then use JavaScript's string logic to extract the text you want.Edit: whoops, probably should read the entire topic first next time Link to comment Share on other sites More sharing options...
sumchurch Posted February 12, 2010 Author Share Posted February 12, 2010 You could theoretically also use AJAX to load the other JavaScript file as plain text, and then use JavaScript's string logic to extract the text you want.Now that's what I have in mind. Where do I start looking for AJAX? Link to comment Share on other sites More sharing options...
just2comment Posted February 12, 2010 Share Posted February 12, 2010 http://www.w3schools.com/ajax/default.asp Link to comment Share on other sites More sharing options...
justsomeguy Posted February 12, 2010 Share Posted February 12, 2010 That's not going to work if the script to load is on a different domain. In that case you would need to use a PHP proxy with ajax, which is why I suggested just using PHP. There's no need to use ajax, you'll always need to use PHP though. e.g.:fileref.setAttribute("src", "get_script.php?file=" + encodeURIComponent("http://www.quotationspage.com/data/mqotd.js")); Link to comment Share on other sites More sharing options...
sumchurch Posted February 13, 2010 Author Share Posted February 13, 2010 That's not going to work if the script to load is on a different domain. In that case you would need to use a PHP proxy with ajax, which is why I suggested just using PHP. There's no need to use ajax, you'll always need to use PHP though. e.g.:fileref.setAttribute("src", "get_script.php?file=" + encodeURIComponent("http://www.quotationspage.com/data/mqotd.js"));Good. I copied the tutorial's sample code to my own, and couldn't even get "test1.txt" or "test2.txt" to work. (Yes, I had those two filenames in the same subdirectory as my htm document.<html><head><script type="text/javascript">function loadXMLDoc(url,id){if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); }else {// code for IE6, IE5 xmlhttp=new ActiveXObject('Microsoft.XMLHTTP'); }xmlhttp.open('GET',url,false);xmlhttp.send(null);document.getElementById(id).innerHTML=xmlhttp.responseText;}</script></head>The bold lines each generated an Error on Page.So on with PHP!Now after:fileref.setAttribute("src", "get_script.php?file=" + encodeURIComponent("http://www.quotationspage.com/data/mqotd.js"));can I put fileref into a variable to slice and dice it?a=fileref; Link to comment Share on other sites More sharing options...
sumchurch Posted February 13, 2010 Author Share Posted February 13, 2010 I found a work-around that will serve my purposes! <script>var b;function getmessage(){a=document.getElementById("message");b=a.innerHTML;return b;}</script></head><body><div id="message"><script language="javascript" src="http://www.quotationspage.com/data/mqotd.js"></script></div><script>getmessage();</script></body></html> There's more, but B now contains the message from the external JS file. The only downside is that the message must be put on the page first -- which I think I can do artfully.Anyway, thanks for all your help with this.Dave Link to comment Share on other sites More sharing options...
sumchurch Posted February 13, 2010 Author Share Posted February 13, 2010 Putting the JS file on the screen and grabbing it into a variable works like a charm! See Visit My Website to see it. The Quote of the Day goes into the floating window, is grabbed into a JS variable, then the regular contents of the float are put there.This <div id></div> and getElementById is simple and very, very powerful! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.