admin管理员组文章数量:1389754
I'm adding a simple script block to a simple aurelia view:
<template>
<script type="text/javascript">
alert('Hello!');
</script>
</template>
The script is never run, even though the view is rendered correctly and I can see that the script block does appear in the DOM.
I have also tried dynamically inserting a script block via the viewModel and also tried with:
<script type="text/javascript" src="http://blah"></script>
I understand it's not best practice to do this, but I'm trying to integrate a third party widget that will then render an iframe. The alert shown above is just a simple way of the verifying the issue that I'm seeing.
The real life scenario is as follows:
- I make an api call to a third party from my view model.
The following is returned:
<script type='text/javascript' language='JavaScript'
src='? ` wid=CBFCIBAA3AAABLblqZhBU33GaMRZ2lMelHKzti7RkanxMP5v- uW_f8CEiKoopNNofJWyXhmE56Su3HTbY*&token=CBNCKBAAHBCAABAARNiZ7Yba0h7dnLaQRBAdTdH9UrJZKryP' />
I need to append this to the DOM and have it execute. I am working around this issue by calling the above url via fetch and then I exec the response, but it seems like a tedious/hacky way of doing it.
I'm adding a simple script block to a simple aurelia view:
<template>
<script type="text/javascript">
alert('Hello!');
</script>
</template>
The script is never run, even though the view is rendered correctly and I can see that the script block does appear in the DOM.
I have also tried dynamically inserting a script block via the viewModel and also tried with:
<script type="text/javascript" src="http://blah"></script>
I understand it's not best practice to do this, but I'm trying to integrate a third party widget that will then render an iframe. The alert shown above is just a simple way of the verifying the issue that I'm seeing.
The real life scenario is as follows:
- I make an api call to a third party from my view model.
The following is returned:
<script type='text/javascript' language='JavaScript'
src='https://secure.na1.echosign./public/embeddedWidget? ` wid=CBFCIBAA3AAABLblqZhBU33GaMRZ2lMelHKzti7RkanxMP5v- uW_f8CEiKoopNNofJWyXhmE56Su3HTbY*&token=CBNCKBAAHBCAABAARNiZ7Yba0h7dnLaQRBAdTdH9UrJZKryP' />
I need to append this to the DOM and have it execute. I am working around this issue by calling the above url via fetch and then I exec the response, but it seems like a tedious/hacky way of doing it.
Share Improve this question edited Apr 9, 2016 at 14:10 Sam Shiles asked Apr 9, 2016 at 13:56 Sam ShilesSam Shiles 11.3k10 gold badges64 silver badges74 bronze badges 01 Answer
Reset to default 6I would remend adapting the solution provided in this answer: Use JS variable to set the src attribute for <script> tag.
In your VM's bind
or attached
method (most likely):
let scriptURL = api.getURL();
let scriptElement = document.createElement('script');
scriptElement.src = scriptURL;
scriptElement.onload = () => {
// do anything you need to do here, or call a VM method
this.someVMMethod();
};
this.scriptElementInHead = document.querySelector('head').appendChild(scriptElement);
If need be, you can even remove the script element by keeping a reference to it and removing it from the head element when the ponent is being unloaded in the unbind
or detached
methods.
detached() {
document.querySelector('head').removeChild(this.scriptElementInHead);
}
本文标签: javascriptScript tag in Aurelia view is not executedStack Overflow
版权声明:本文标题:javascript - Script tag in Aurelia view is not executed - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744742466a2622689.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论