17

Is there a way to make the below script pass the javascript values to the url of the href link?

<script type="text/javascript">
function geoPreview(lat,long) {
var elemA = document.getElementById("lat").value;
var elemB = document.getElementById("long").value;

window.location.href = "http://www.gorissen.info/Pierre/maps/googleMapLocation.php?lat=elemA&lon=elemB&setLatLon=Set";

}
</script>
vsapountzis
  • 618
  • 3
  • 11
  • 28

5 Answers5

43

Try this:

 window.location.href = "http://www.gorissen.info/Pierre/maps/googleMapLocation.php?lat="+elemA+"&lon="+elemB+"&setLatLon=Set";

To put a variable in a string enclose the variable in quotes and addition signs like this:

var myname = "BOB";
var mystring = "Hi there "+myname+"!"; 

Just remember that one rule!

pattyd
  • 5,927
  • 11
  • 38
  • 57
  • 7
    Don't stick arbitrary strings directly into a URL. You need to encode them properly with `encodeURIComponent` – Quentin Feb 21 '17 at 11:04
  • This is a bad practice, do not perform this action without proper sanitisation. – Ray Jun 26 '20 at 16:27
  • 2
    Would be nice to see the correct way to do that if this is retained to be bad practice. – Garavani Jul 15 '21 at 07:33
4

Do you mean include javascript variable values in the query string of the URL?

Yes:

 window.location.href = "http://www.gorissen.info/Pierre/maps/googleMapLocation.php?lat="+var1+"&lon="+var2+"&setLatLon="+varEtc;
Drew
  • 4,215
  • 3
  • 26
  • 40
  • 2
    Don't stick arbitrary strings directly into a URL. You need to encode them properly with `encodeURIComponent` – Quentin Feb 21 '17 at 11:04
4

Summary

With either string concatenation or string interpolation (via template literals).

Here with JavaScript template literal:

function geoPreview() {
    var lat = document.getElementById("lat").value;
    var long = document.getElementById("long").value;

    window.location.href = `http://www.gorissen.info/Pierre/maps/googleMapLocation.php?lat=${lat}&lon=${long}&setLatLon=Set`;
}

Both parameters are unused and can be removed.

Remarks

String Concatenation

Join strings with the + operator:

window.location.href = "http://www.gorissen.info/Pierre/maps/googleMapLocation.php?lat=" + elemA + "&lon=" + elemB + "&setLatLon=Set";

String Interpolation

For more concise code, use JavaScript template literals to replace expressions with their string representations. Template literals are enclosed by `` and placeholders surrounded with ${}:

window.location.href = `http://www.gorissen.info/Pierre/maps/googleMapLocation.php?lat=${elemA}&lon=${elemB}&setLatLon=Set`;

Template literals are available since ECMAScript 2015 (ES6).

FlashOver
  • 1,855
  • 1
  • 13
  • 24
1

This is the way to set your variable in url

var roomname = '2nxjoinroomnot9480';
window.location.replace("/login?roomid=" + roomname);

In and in the give page URL you can get variable like this

const roomId = new URLSearchParams(window.location.search).get('roomid');
console.log(roomId);
General Grievance
  • 4,555
  • 31
  • 31
  • 45
-1

Try this:

window.location.href = "http://www.gorissen.info/Pierre/maps/googleMapLocation.php?lat=\''+elemA+'\'&lon=\''+elemB+'\'&setLatLon=Set";
Avnesh Shakya
  • 3,828
  • 2
  • 23
  • 31