does anyone know how to load the js external file from javascript? i tried it on google to find the working one but none of them are working. please help!!!!
No I am only using javascript nothing else. I tried to read test1.js from key.js, but it won't letting me to read it. Here is the one i'm currently using it in my javascript: key.js function checkloadjscssfile(filename, filetype) { loadjscssfile(filename, filetype) filesadded+="["+filename+"]" //List of files added in the form "[filename1],[filename2],etc" } document.onkeydown = function(ev) { var key; ev = ev || event; key = ev.keyCode; var image1 = document.getElementById("image1").getElementsByTagName("img")[0]; if(key == 13) { if (img1.indexOf('image1_yellow.jpg') != -1) { if (img5.indexOf('picture1_yellow.jpg') != -1) { loadjscssfile("test1.js", "js"); } } } Code (markup): test1.js var teststring="hello! you are reading another javascript file"; alert("teststring); Code (markup): I can't read the test1.js from the key.js. any idea?
sorry, I have added the wrong code when I posted it. I used this code to read my javascript code but it did not work. function loadjscssfile(filename, filetype) { var fileref=document.createElement('script'); fileref.setAttribute("type","text/javascript"); fileref.setAttribute("src", filename); } document.onkeydown = function(ev) { var key; ev = ev || event; key = ev.keyCode; var image1 = document.getElementById("image1").getElementsByTagName("img")[0]; if(key == 13) { if (img1.indexOf('image1_yellow.jpg') != -1) { if (img5.indexOf('picture1_yellow.jpg') != -1) { loadjscssfile("test1.js", "js"); } } } Code (markup): any idea?
Errors: 1. you didn't append the script to the body element 2. test1.js had an extra quote. Bad Practice: 1. Using setAttribute. key.js function loadjscssfile(filename, filetype) { var fileref=document.createElement('script'); fileref.type = "text/javascript"; fileref.src = filename; document.body.appendChild(fileref); } document.onkeydown = function(ev) { var key; ev = ev || event; key = ev.keyCode; if(key == 13) { if (img1.indexOf('image1_yellow.jpg') != -1) { if (img5.indexOf('picture1_yellow.jpg') != -1) { loadjscssfile("test1.js", "js"); } } } } HTML: test1.js var teststring="hello! you are reading another javascript file"; alert(teststring); HTML:
thank you very much for your help, it is working now. I have a problem with the function I created. I tried to read them but nothing is going to happens and the alert message box did not display. key.js function loadjsfile(filename) { var fileref=document.createElement('script'); fileref.setAttribute("type","text/javascript") fileref.setAttribute("src", filename); document.body.appendChild(fileref); } document.onkeydown = function(ev) { var key; ev = ev || event; key = ev.keyCode; var image1 = document.getElementById("image1").getElementsByTagName("img")[0]; if(key == 13) { if (img1.indexOf('image1_yellow.jpg') != -1) { if (img5.indexOf('picture1_yellow.jpg') != -1) { loadjscssfile("test1.js", "js"); } } } Code (markup): test1.js function myFunction() { alert("hello, this is a test"); } Code (markup): do you know why and what is wrong?
1. You have to change "loadjscssfile" to "loadjsfile" 2. Below the line with appendChild, insert a new line with: myFunction();
I have already got it working. Now I want to work on to read the code from php using with javascript. do you know how i can read the code from my php using with javascript?