使用javascript从iframe检索值 [英] using javascript retrieving value from iframe

查看:60
本文介绍了使用javascript从iframe检索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var url="http://api.ipinfodb.com/v3/ip-city/?key=9a9791a9d6079663d802274e09dcb8170bf98fdfaa0eb37da604d1d83d5deb56&ip=72.32.84.70";
        var response = null;
        var connection = new XMLHttpRequest();
        try
        {
            connection.open("GET", url, false);
           connection.send();
            if(connection.readyState == 4&& connection.status==200) response = connection.responseText;
        }
        catch(e)
        {
           alert("ERROR: The remote host could not be found or the connection was refused.");
            return false;
        }
        alert(response);



重要的是url具有文本值(我的需要),我看着url(页面源)没有html或body或任何元素.但是有文字.

我尝试了innerhtml,body,values,但所有显示为空.

我的想法可能是将URL强制转换或嵌入到文本文档或Word文档中.但是我需要在javascript变量中使用该值.



Important thing url is having text value (my need) , I watched the url (page source) not having html or body or any element. But having the text.

I tried innerhtml,body,values but all displays empty.

My thought is may be that url is casted or embeded with a text document or word document. But i need that value in a javascript variable.

Please if anybody crossed this help me.

推荐答案

这里是问题所在:您当然可以做到,但可以异步进行.就是说,当您开始通过Ajax加载iframe源页面时,它不会阻止脚本的执行,因此进一步执行可能会覆盖回调的结果.

我更喜欢用jQuery.考虑一下:

Here is the problem: you can do it of course, but asynchronously. That said, when you start loading the iframe source page by Ajax, it won''t block execution of the script, so further execution might override the result of your callback.

I prefer doing it with jQuery. Consider this:

<html>
<head>
    <title></title>
    <script type="text/javascript" src="scripts/jquery-1.7.1.min.js"></script>
</head>
<body>

    <!-- let's assume test.html contains the string 'Ajax does work!' -->
    <iframe id="myIframe" src="test.html"></iframe>

    <script type="text/javascript">


(document).ready(function(){ var内容; var iFrameSource =
(document).ready(function() { var content; var iFrameSource =


(#myIframe").attr('src')//将分配"test.html"
("#myIframe").attr('src') //will assign "test.html"


这篇关于使用javascript从iframe检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆