如何在javascript:open链接中正确嵌入GEO_LATITUDE和GEO_LONGITUDE变量值
Fixing Your JavaScript Geolocation Link
First, let's break down the key issues in your current code that are preventing the variables from being embedded correctly:
- You’re using
#GEO_LONGITUDE#and#GEO_LATITUDE#, which isn’t valid JavaScript syntax for inserting variables—we need to use string concatenation with the+operator instead. - The string quotes are mismatched: you close the URL string too early before adding the variables, causing syntax errors.
- There’s an extra
','+', 'segment that breaks the flow of the URL construction. window.innerheighthas a typo—it should bewindow.innerHeight(capital "H" is required for this browser API).- You’re missing a comma between the
widthandresizableproperties in the window features string.
Here’s the corrected code that properly inserts your variables and fixes all the syntax issues:
javascript:open('http://maps.google.com/maps?q=' + GEO_LONGITUDE + '+' + GEO_LATITUDE, 'popup-example', 'height=' + window.innerHeight + ',width=' + window.innerWidth + ',resizable=no')
How this works:
- We start the URL string with
'http://maps.google.com/maps?q=' - Concatenate the
GEO_LONGITUDEvariable using+ - Add the required
+separator as a string ('+') - Concatenate the
GEO_LATITUDEvariable to complete the query parameter - Fix the window feature string by correcting the
innerHeighttypo and adding the missing comma beforeresizable=no
For your example values (46.15 and 50), this code will generate exactly the link you want:
javascript:open('http://maps.google.com/maps?q=46.15+50', 'popup-example', 'height='+window.innerHeight+',width='+window.innerWidth+',resizable=no')
Just double-check that GEO_LATITUDE and GEO_LONGITUDE are properly defined variables in the context where this link runs—otherwise, you’ll see undefined in the URL instead of your values.
内容的提问来源于stack exchange,提问作者Andrii Havrylyak
相关产品推荐
相关产品推荐

