2017-12-21 15 views
0

htmlの本文内のhref属性にfinalNameの値を渡したいとします。どうすればいいですか?javascriptの値をHTML href属性に渡すにはどうすればいいですか?

<html> 
<script type="text/javascript" language="Javascript"> 
     var url = window.location.search; 
     var val = getParameter('n'); 
     var finalName = ""; 
     if(val == false) { 
      var a= prompt("Enter your name & see the magic", ""); 
      if(a){ 
       window.location.href = '?n='+a; 
       document.getElementById("demo2").innerHTML = a ; 
       finalName = a; 
      } 
     } 
     else { 
      document.getElementById('demo2').innerHTML = val; 
     } 
</script> 

<body> 
<a class="footerbtn" href="whatsapp://send?text= send you special surprise message%0A%0ATouch this blue line %0Aenter your name %0A http://magic.pagalworld.buzz/?n=" 
    onclick="location.href=this.href+finalName;return false;"><img width="25px" height="25px" src="wp.png"/><b style="font-size: 15px;"> Click Here to Share on Whatsapp</b> <img width="25px" height="25px" src="wp.png"/></a> 
</body> 
</html> 
+0

あなたはそれがページをリロードすると、現在のページストップ運転上のスクリプトwindow.location' 'に割り当てます。 – Barmar

+0

this.href + '?data =' + finalName – Kalabalik

+0

GETリクエストで変数を渡す 'window.location'を変更した場合は、このリクエストを処理するためのサーバ言語が必要になります。その必要はありません。 –

答えて

0

変数をwindow.finalNameを使用してアクセス可能にすることができます。 例:

<script type="text/javascript" language="Javascript"> 
 
     var url = window.location.search; 
 
     var val = getParameter('n'); 
 
     window.finalName = ""; 
 
     if(val == false) { 
 
      var a= prompt("Enter your name & see the magic", ""); 
 
      if(a){ 
 
       window.location.href = '?n='+a; 
 
       document.getElementById("demo2").innerHTML = a ; 
 
       window.finalName = a; 
 
      } 
 
     } 
 
     else { 
 
      document.getElementById('demo2').innerHTML = val; 
 
     } 
 
</script>
<html> 
 

 

 
<body> 
 
<a class="footerbtn" href="whatsapp://send?text= send you special surprise message%0A%0ATouch this blue line %0Aenter your name %0A http://magic.pagalworld.buzz/?n=" 
 
    onclick="location.href=this.href+finalName;return false;"><img width="25px" height="25px" src="wp.png"/><b style="font-size: 15px;"> Click Here to Share on Whatsapp</b> <img width="25px" height="25px" src="wp.png"/></a> 
 
</body> 
 
</html>

+0

これはグローバル変数で、自動的に 'window'のプロパティです。 – Barmar

関連する問題