简体   繁体   中英

Get text between tags in iFrame

I'm loading the website inside of an iFrame. I'm trying to get the text between <carrier_name> and </carrier_name> and display that inside of a textbox.

This is my HTML

 <iframe name="frameComp" class="frameComp" style="display:none" width="500" height="200"></iframe>

This is my javascript

 $('button[name="otherlistbut"]').click(function() {
     var phonenum = $('input[name="phonenum"]').val();
     $(".frameComp").fadeIn();
     window.open("https://api.data24-7.com/v/2.0?user=USERNAME&pass=PASSWORD&api=C&p1=" + "1" + phonenum, "frameComp");

     var message = $('textarea[name="message"]').val();
     message ==  $('.frameComp').contents().find("carrier_name").html();

});

This is the source code of the website

<?xml version="1.0" ?>
<response>
    <results>
        <result item="1">
            <status>OK</status>
            <number>13105555555</number>
            <wless>y</wless>
            <carrier_name>Verizon Wireless</carrier_name>
            <carrier_id>5</carrier_id>
            <country>United States</country>
        </result>
    </results>
</response>

OK, different approach...

$('button[name="otherlistbut"]').click(function() 
{
    var phonenum = $('input[name="phonenum"]').val();

    $('<iframe id="frameComp" src="https://api.data24-7.com/v/2.0?user=USERNAME&pass=PASSWORD&api=C&p1=" + "1" + phonenum>').appendTo("body").ready(function()
    {
        // Wait until XML has actually loaded by testing if the required (known) element is there
        var interval = setInterval(function()
        {
            var carrier = $('#frameComp').contents().find("carrier_name")
            if(carrier.length > 0)
            {
                console.log(carrier.text());
                clearInterval(interval);
                $('#frameComp').remove(); // We no longer need the iframe...
            }
        }, 100);
    });
});

Hope this helps!

<?php 

$phonenum = $_POST["phonenum"];
$url = "http://api.data24-7.com/v/2.0?user=USERNAME&pass=PASSWORD&api=C&p1=" . "1" . $phonenum;

$provhtml = file_get_contents($url);
$start_tag = '<carrier_name>';
$end_tag = '</carrier_name>';

$startpos = strpos($provhtml, $start_tag) + strlen($start_tag);
if ($startpos !== false) {
    $endpos = strpos($provhtml, $end_tag, $startpos);
    if ($endpos !== false) {
        $carrier = substr($provhtml, $startpos, $endpos - $startpos);
    }
}
echo json_encode($carrier);

 ?>

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