简体   繁体   中英

Get embedded values in javascript function on HTML Page

I am using php and trying to fetch a value in a javascript function. The javascript in the HTML page is the following:

<script type="text/javascript">

        function show(q)
        {
            if(q.length > 0)
            {           
                if(q.indexOf("tid") > -1)
                {
                    location.href = "map.jsp?" + q;
                }
                else
                {
                    location.href = "listmap.jsp?" + q;
                }
            }

            return false;
        }

        $(function() { 
            $("#map").goMap({ 
                latitude: -35.331415
                ,longitude: 149.131851
                ,maptype: 'ROADMAP'
                ,scrollwheel: false
                ,zoom: 11
                ,markers: [ {latitude: -35.192103,longitude: 149.332313,icon: 'images/m/marker1.png',html: {content: 'Loading...', ajax: 'mapinfo.jsp?sid=9057'}}]

I need to get the values of (3rd and 4th lines in second function) the latitude and longitude which are: -35.331415 and 149.131851

I'm just not sure how to access the individual values...

EDIT: I AM USING PHP to save these values in variables then later write them to a csv file Currently i'm using this code (here's a snippit) to get everything else on other pages

<?php


$ch = curl_init("http://pvoutput.org/listmap.jsp?sid=312");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$cl = curl_exec($ch);


$dom = new DOMDocument();
@$dom ->loadHTML($cl);

$Lat_data = $dom -> getElementsByTagName("script");
print_r($Lat_data -> item(1) );

EDIT 2: I fixed all the problems by doing this:

$Lat_data = $dom -> getElementsByTagName("script");
$content =$Lat_data -> item(10) -> textContent;

$regex = "/latitude: ([^\b,]*)/"; 
if (preg_match( $regex , $content , $values )) {
echo  $values[1];
$regex2 = "/longitude: ([^\b,]*)/";
}
if (preg_match($regex2, $content, $values2)) {
echo $values2[1];
}

You managed to get the script node. Now you want its content (the javascript code)

$content = $Lat_data -> item(1) -> textContent

Once you got the content you can parse it using regex expression to get what you want

$regex = "/latitude: ([^\b\n]*)(.|\n)*,longitude: ([^\b\n,]*)/";
if (preg_match($regex, $content, $values)) {
   echo 'latitude: ' . $values[0];
   echo 'longitue: ' . $values[2];
}

regex explanation (test here ):

  • finds "latitude: "
  • then we get the first group (), [^\\b\\n]* means anything but blackspace or newline characters
  • get anything between latitude value and ',longitude: '
  • next find ",longitude: "
  • third group just as the previous one. I added ',' so that if there is not blank between the longitude and ', maptype' it still works.

this regex explanation is getting a little messy, check the test link to more detailed explanation.

I have not tested it so there might be some syntax error but the idea is there ;)

实施一些ajax将js文件中的经度和纬度提交到php文件中,效率会更高吗?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM