2017-12-21 2 views
0

Ich möchte den Wert von finalName im href-Attribut im HTML-Text übergeben. Wie kann ich das tun?Wie übergeben Sie Javascript-Wert an HTML-Attribut?

<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

Wenn Sie zuweisen 'window.location' es die Seite neu geladen und Skripte auf der aktuellen Seite Anschlag läuft. – Barmar

+0

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

+0

Wenn Sie 'window.location' ändern und Ihre Variable in einer GET-Anfrage übergeben, benötigen Sie eine Serversprache, um diese Anfrage zu bearbeiten. Das ist nicht nötig. –

Antwort

0

Sie können die Variable mit window.finalName zugänglich machen. zB:

<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

Es ist eine globale Variable, sie sind automatisch Eigenschaften von 'Fenster'. – Barmar

Verwandte Themen