Elydasian
Elydasian

Reputation: 2066

Javascript how to create a script tag dynamically without the source file?

My debug.html page got this:

window.onload = function() {
    var dataFromLink = parseData(location.search.substring(5));
    callScript(dataFromLink);
};

</script>

And my debugBoot.js got this:

function parseData(dataFromLink){
    dataFromLink=dataFromLink.replace(/%20/g, ' ');
    dataFromLink=dataFromLink.replace(/%22/g, '"');
    dataFromLink=dataFromLink.replace(/%27/g, '\'');
    dataFromLink=dataFromLink.replace(/%3C/g, '<');
    dataFromLink=dataFromLink.replace(/%3E/g, '>');
    return dataFromLink;
}

function callScript(param1) {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.innerHTML = param1;
    script.id = "debugScript";
    script.src = "debugScript.js";
    document.getElementsByTagName("head")[0].appendChild(script);
    return false;
}

And my data from the location.search.substring(5) looks like this when I load the page but with staff I get rid of in parseData function:

function execute(doSave) { 
    dss.login(userId);
    return true;
}

So, I have a web application, where I got a folder WEBContent, and there is my file debugScript.js

My code reads the file from there with the following line:

script.src = "debugScript.js";

and that works, but I have a problem...

The link, that I use to come to the page where I have this code, triggers also a function that saves the dataFromLink to the file, from where I call the data. That works just fine, but there is a problem. Always when the function saves the data, I need to refresh the eclpise file so that my script can read it, if I don't refresh the file, its just its old version.

I tried to save it to local disk, but then I get an error from the browser

Not allowed to load local resource: file://...

Long story short, I need somehow to create a script without to read any file, but to insert an empty script into the page, and then to write the stuff from dataFromLink to it...

Is it possible?

or maybe to create a temporary file somewhere, but not on the server from witch I can read?

I would accept any solution :)

Upvotes: 4

Views: 4327

Answers (1)

Elydasian
Elydasian

Reputation: 2066

Forgot to answer :)

Ok, the problem I had there, could not be solved the way I wanted.

What I did in the end was, that I used the source from the page one, where the textarea was, and created a javascript file, whose name I sent as a parameter to the second page, witch then could find the file in the file system and open it like this:

<script id=loadScript>
    window.onload = function() {    
        callScript(fileName, userId);   
    };
</script>


function callScript(fileName, userId) {
    var script = document.createElement("script");
    script.type = "text/plain";
    script.id = "debugScript";
    script.src = ".../getSource/" + userId + "/" + fileName;
    document.getElementsByTagName("head")[0].appendChild(script);
}

Upvotes: 1

Related Questions