只需要一个 html url 中的变量参数,如下所示:
mywebpage.com/something.html?width=(Parameter1)&height=(Parameter2)
但是在该网页的内容中,我想添加以下内容:
<script>fid="example"; width=(Parameter1); height=(Parameter2);</script>
我怎样才能只使用 javascript 和 Html
只需要一个 html url 中的变量参数,如下所示:
mywebpage.com/something.html?width=(Parameter1)&height=(Parameter2)
但是在该网页的内容中,我想添加以下内容:
<script>fid="example"; width=(Parameter1); height=(Parameter2);</script>
我怎样才能只使用 javascript 和 Html
这是类似问题中建议的代码(如何从 GET 参数中获取值?)
var QueryString = function () {
// This function is anonymous, is executed immediately and
// the return value is assigned to QueryString!
var query_string = {};
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
// If first entry with this name
if (typeof query_string[pair[0]] === "undefined") {
query_string[pair[0]] = pair[1];
// If second entry with this name
} else if (typeof query_string[pair[0]] === "string") {
var arr = [ query_string[pair[0]], pair[1] ];
query_string[pair[0]] = arr;
// If third or later entry with this name
} else {
query_string[pair[0]].push(pair[1]);
}
}
return query_string;
} ();
只要变量列表有些固定且数量很少,请保持简单。
HTML:
<a href="#" id="some-link-id>Click me!</a>
JS:
var width = 100;
var height = 100;
var link = document.getElementById('some-link-id');
var url = "http://mywebpage.com/page.html?width=" + width + "&height=" + height;
link.href = url;
或者要更新您所在页面的 URL,只需设置window.location.href
var width = 100;
var height = 100;
var url = "http://mywebpage.com/page.html?width=" + width + "&height=" + height;
window.location.href = url;