javascript - 我正在努力传递一个 url 变量以用作动态生成的嵌入的变量
问题描述
我之前曾寻求帮助,但很遗憾仍然遇到问题,得到了@tejashwi-kalp-taru 的帮助
我想简单地将 url ?variable=1234 的变量传递给嵌入脚本标签,以便在基于变量的动态嵌入中使用
我得到了帮助并建议创建一个我包含的功能,但它仍然无法正常工作
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script type="text/javascript">
// Parse the URL parameter
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g,"\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
// Give the parameter a variable name
var myvariable = getParameterByName('variable');
$('#myvariable').html(myvariable);
function addScript( src, value ) {
var s = document.createElement( 'script' );
s.setAttribute( 'src', src );
s.setAttribute( 'myvalue', value);
document.body.appendChild( s );
}
<div id="otEmbedContainer" style="width:800px; height:640px"></div>
addScript('https://example.com/embed/embed/ot-embed.js?
embedId=5432&variable=', myvariable);
</script>
解决方案
不确定“仍然无法正常工作”的真正含义,但是您的代码中存在严重问题.....
当你混合它们时,你必须确保“弹出/弹出”正确的语言!
在您的示例中,您没有从 JS 正确“弹出”到 HTML .....
这是相同的代码,更正和清理了一下(并进行了评论,以便您可以看到我做了什么):
<!-- first, do all the HTML (generally, keep it above the JS, though there are times you have to do it differently - but for this example, it should be OK) -->
<div id="otEmbedContainer" style="width:800px; height:640px"></div>
<!-- Now, you will go into JS and do all the JS things-->
<script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<script type="text/javascript">
// set up your variables first - keeping them together
// Give the parameter a variable name
var myvariable = getParameterByName('variable');
// NOTE: in your example, id of 'myvariable' is not shown, so there may be some issues there????
$('#myvariable').html(myvariable);
// Parse the URL parameter
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g,"\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
function addScript( src, value ) {
var s = document.createElement( 'script' );
s.setAttribute( 'src', src );
s.setAttribute( 'myvalue', value);
document.body.appendChild( s );
}
addScript('https://example.com/embed/embed/ot-embed.js?embedId=5432&variable=', myvariable);
</script>
希望这可以帮助您实现这一目标。使用多种语言时,您只需要记住您所在的语言并确保“弹出”到另一种语言(当您将 PHP 添加到 HTML/JS 混合时,有时事情会变得复杂,但同样的规则适用 - 是通过弹出/弹出并一次迈出一步,确保您使用正确的语言!
快乐编码!
推荐阅读
- reactjs - 失败的道具类型:提供给路线的“对象”类型的无效道具“组件”,预期的功能?
- python - Python PIP 尝试更新失败
- javascript - 如何使用 onclick 按钮重新调用生成函数
- javascript - 由于参考错误导致项目测试失败 - React
- python - 大内存处理的循环执行时间不一致
- pandas - 按 Pandas 中的行索引分组
- java - else if 语句中的 input.equals 不能按预期工作
- html - 背景图像未正确调整到屏幕大小
- automation - 为什么使用 Tosca throwing 的自动化数据驱动测试用例 - 没有为命令“XL”找到合适的值
- javascript - 处理退货声明,但不理解!操作员