简体   繁体   中英

ADD Html URL Parameters with Javascript

Just want a variable parameter in an html url like this:

mywebpage.com/something.html?width=(Parameter1)&height=(Parameter2)

But in the content of that webpage i want to add this:

<script>fid="example"; width=(Parameter1); height=(Parameter2);</script>

How can i do it using only javascript and Html

Here is code suggested from similar question ( How to get the value from the GET parameters? )

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;
} ();

So long the list of variables is somewhat fixed and small in quantity, keep it simple.

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;

Or to update the URL of the page you are on, simply set window.location.href

var width = 100;
var height = 100;

var url = "http://mywebpage.com/page.html?width=" + width + "&height=" + height;
window.location.href = url;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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