Change JavaScript variable value by passing new values inside URL ( GET request )

谁都会走 提交于 2021-02-10 15:16:41

问题


I have an i frame which has a src called map.php.

  <iframe src= "map.php " ></iframe>

Inside map.php i have a variable called map. I want to change the value of "center " inside the map variable to something new when the src load up

map.php (javascript)

var map = new mapboxgl.Map({


 center: [115.83333, -32.01667], 
    
 
});

So my question is whether i can pass some parameter values along with src = map.php to change the center value

example. ( Just my concept )

 <iframe  src= "map.php/parameter center[new values] " ></iframe>

回答1:


you can pass values in query param by constructing the url like this:

map.php/parameter?cx=115.83333&cy=-32.01667

Again in the php script you can access these values from query params using $_SERVER['QUERY_STRING']. Just parse,extract and use the values inside php file.




回答2:


What @diwakersurya said was right , but you had to use the GET. I am posting a full answer for anyone who is stuck.

php

$str1 = $_SERVER['QUERY_STRING'];
$x = $_GET['cx'];
$y = $_GET['cy'];


来源:https://stackoverflow.com/questions/66057989/change-javascript-variable-value-by-passing-new-values-inside-url-get-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!