gpt4 book ai didi

javascript - 如何使用 angularjs 变量在jsp页面中动态更改脚本标签的路径? (可能重复)

转载 作者:行者123 更新时间:2023-12-03 02:43:52 25 4
gpt4 key购买 nike

这是我的问题:

我有一个WEB项目我有一个主页,在那里我可以打开一个新选项卡,新选项卡会自动加载外部 JavaScript 文件。

现在,在本地主机上一切正常,并且应该如何工作一旦我在本地 wildFly 服务器上部署应用程序并在不同的 URL (something.something/somethingSpecial) 上独立运行它

我的 URL 末尾有一个文件夹(不是我的),在我的代码中,我需要后退一步才能导入我的脚本。

现在我需要一种简单的方法来使用环境.生产选项(angularjs 4)设置脚本标记的路径

这是我的选项卡 JSP 页面的代码:

//what I have
<script
src="rest/project1/getSource/${param.userId}/${param.fileName}">
</script>

<script>
// I send the production boolean with the link that opens the tab
// and just split it out of it :)
var production = location.search;
production = production.split('&production=')[0] // this is true or false (tested, and it's 100% working)
</script>

现在,我需要这样的东西:

<script 
// I need to go one folder back, and do the same as above
src="../rest/project1/getSource/${param.userId}/${param.fileName}">
</script>

我可以只编写第二个代码,然后就可以完成它,因为它可以在服务器上工作,但它不会在我的本地主机上工作,而且我不能拥有

所以,我想两者兼得,如下所示:

<script 
src=if (production) then "../rest/project1/getSource/${param.userId}/${param.fileName}" else "rest/project1/getSource/${param.userId}/${param.fileName}">

我尝试过类似的方法,但无法使其工作......

src={{production && '../rest/project1/getSource/${param.userId}/${param.fileName}' || 
'rest/project1/getSource/${param.userId}/${param.fileName}'}}
</script>

我错过了什么?

最佳答案

选择脚本元素。将 Id 属性添加到您的脚本元素(完全没问题)。

HTML

<script id="script" src="rest/project1/getSource/${param.userId}/${param.fileName}"></script>

JavaScript

function changeSource(production){
var path = production ? "../rest/project1/getSource/${param.userId}/${param.fileName}" : "rest/project1/getSource/${param.userId}/${param.fileName}";
document.getElementById("script").setAttribute('src', path);
}

页面加载之前:( Angular )

JavaScript

var path = production ? "../rest/project1/getSource/${param.userId}/${param.fileName}" : "rest/project1/getSource/${param.userId}/${param.fileName}";

DOM

<script src="{path}"></script>

关于javascript - 如何使用 angularjs 变量在jsp页面中动态更改脚本标签的路径? (可能重复),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48189815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com