读取javascript变量并添加到html iframe src

时间:2013-12-31 15:46:53

标签: javascript jquery html iframe

在下面的代码中,我有2个JavaScript变量long和lat

我的挑战是能够将这些值附加到iframe网址的末尾

我需要对下面的代码进行更改以帮助我实现上述目标,我们将不胜感激

下面的iframe代码我需要能够用js变量lat long替换数值“-37.718821,145.064612”,下面的代码从页面url动态确定

<!DOCTYPE html>
<html>
    <head>
        <title>jQM Complex Demo</title>
        <meta name="viewport" content="initial-scale=1, maximum-scale=1"/>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
        <script>

            function gup(name) {
                name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
                var regexS = "[\\?&]" + name + "=([^&#]*)";
                var regex = new RegExp(regexS);
                var results = regex.exec(window.location.href);
                if (results == null)
                    return "";
                else
                    return results[1];
            }
            var latlong = (gup('q'));
            var parts = latlong.split(",");
            var lat = parts[0];
            var long = parts[1];


        </script>



    </head>
    <body>

        <div data-role="page" id="map_display_Page">

            <div data-role="header" id="myheader" data-position="fixed" style="background: #ffffff"></div>
            <div data-role="header" id="myheader" data-position="fixed" style="color: #ffffff; background: #000033;">
                <a href="#" data-role="button" data-rel="back" data-icon="back">Back</a>
                <h1>Map Location</h1>
            </div>

            <div data-role="content">
                <iframe src="maps1.html?q=-37.718821,145.064612" style="position:fixed; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%; border:none; margin:0; padding:0; overflow:hidden;"></iframe>  --->          </div>

            <div data-role="footer" data-theme="a" data-position="fixed" data-id="myfooter" style="color: #ffffff; background: #000033;">
                <div class="ui-bar">
                    <a href="sharedialog.html"  data-role="button" data-icon="star" data-theme="a" data-rel="dialog">Share</a>
                    <a href="mailto:info@TrackingCentral.com.au?subject=Iphone App Enquiry" data-role="button" data-icon="plus" data-theme="a">Contact</a>
                    <a href="" data-role="button" data-icon="arrow-u" data-theme="a" style="float:right;" class="returnTopAction">Return top</a>
                </div>
            </div>
        </div>
    </body>
</html>

2 个答案:

答案 0 :(得分:0)

试试这个:

$("iframe").attr("src", "maps1.html?q="+ lat +"," + long +");

这只会更改iframe的src属性,并将其替换为变量latlong

答案 1 :(得分:0)

试试这个应该有效

$(document).ready(function(){
$("iframe").attr("src","maps1.html?q="+lat+","+long);
});

$("iframe").src="maps1.html?q="+lat+","+long;