简体   繁体   English

通过Java脚本将Java脚本添加和加载到浏览器中,而无需重新加载

[英]Adding and Loading Javascript to a Browser via Javascript without reloading

I have a SWT Browser in an Eclipse RAP Application, where i load something on my local server and display that html. 我在Eclipse RAP应用程序中有一个SWT浏览器,在其中我在本地服务器上加载了一些东西并显示该html。

the url looks something like this: " http://localhost:port/......./myhtml.html " 网址看起来像这样:“ http:// localhost:port /......./ myhtml.html

I want to add script sources into my html without reloading the page. 我想在不重新加载页面的情况下将脚本源添加到html中。 Currently i am doing this with a Javascript which i execute in the Browser. 目前,我正在使用在浏览器中执行的Javascript执行此操作。

var script = document.createElement('script');
script.setAttribute('src', 'SOMESOURCE');
document.head.appendChild(script);

This does work that i end up having the script tags correctly added to the HTML DOM, however it does not load the references, so i can't access anything defined in these script references. 这确实可以使我最终将脚本标签正确添加到HTML DOM,但是它不会加载引用,因此我无法访问这些脚本引用中定义的任何内容。 But calling something like 但是调用类似

window.top.location.reload(false)

doesnt work either because this reloads the HTML and therefor removing my inital added script tags. 也不起作用,因为这会重新加载HTML,因此删除了我最初添加的脚本标签。

The main problem here is that i am very restricted because of the tecnologies we are using here, so i am only able to execute javascript queries in my browser. 这里的主要问题是,由于我们在这里使用的技术,我受到很大的限制,所以我只能在浏览器中执行javascript查询。

PS: For testing you just can open any Browser like Chrome and Firefox, open its Developer Toolkit and type that script into the console. PS:要进行测试,您只需打开任何浏览器(如Chrome和Firefox),打开其Developer Toolkit,然后在控制台中键入该脚本即可。 The website doesn't matter. 网站没关系。

Thanks for @JensV for helping me. 感谢@JensV对我的帮助。

The Problem was although i added my script it didnt load its content. 问题是尽管我添加了我的脚本,但它没有加载其内容。 So what i was as described in the question. 因此,我在问题中所描述的是什么。

However as mentioned from @JensV in the Bug 但是正如@JensV在Bug中提到的那样

load scripts asynchronously 异步加载脚本

it is described to use a Promise object this would handle the load and error events. 它被描述为使用Promise对象来处理加载和错误事件。

function loadScript(src) {
    return new Promise(function (resolve, reject) {
        var s;
        s = document.createElement('script');
        s.src = src;
        s.onload = resolve;
        s.onerror = reject;
        document.head.appendChild(s);
    });
}

So what i did was first execute that Javascript and therefor adding this function, and then just execute it with the desired source. 因此,我要做的是先执行该Javascript,然后添加此函数,然后仅使用所需的源执行它。

Thanks for @JensV again, you might wanna flag this as duplicate, however i think my problem was a bit different then that. 再次感谢@JensV,您可能想将此标记为重复,但是我认为我的问题与那有些不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM