Append URL into html

2019-09-21 09:01发布

My website URL is:- http://16005ee.blogspot.in/2017/10/urlnametest.html?n=user.name

So I want to extract that user name from URL into html but I can't get it

I actually had tried it already but it is not working

Please help me

Html code

<P id="demo"><p>

Script code:-

<script>
document.getElementById("demo").innerHTML =
"Welcome " + editors + " to my website";

function() {
  var url_string = window.location.href;
  var url = new URL(url_string);
  var editors = url.n.get('editors');
  console.log (editors);
}
</script>

2条回答
别忘想泡老子
2楼-- · 2019-09-21 09:37

Based on fact that this is GET parameter you can do it like that:

(function() {
  var url_string = window.location.href;
  var url = new URL(url_string);
  var editors = url.searchParams.get('editors');
  var htmlElement = document.getElementById("element");
  htmlElement.innerHTML += editors;
})();

Where in your case your searchParams is n.

After you get this element you can easily append your html code.

EDIT I've added example with appending html element.

查看更多
beautiful°
3楼-- · 2019-09-21 09:49

I know that I am late, but from Czeran's answer, I created this JS function that will append and go to the URL:

    function urlAppend(value){
        var current_location = window.location.href;
        current_location += "/" + value;
        window.location.href = current_location;
    }

This allows for a more dynamic URL append

Hope this helps!

查看更多
登录 后发表回答