开发者

javascript: GoToPage for my iframe with a remote source

I have a feed from Coupons.com in the form of an iframe in my webpage and I want to run some JavaScript on this iframe. I want it to open on a page other than the default which is the first page.

If you go to http://www.coupons.com you can see the same feed I am working with.

Ab开发者_运维百科ove the coupons is a list of pages 1-10. If you move the cursor over page 3 (for example) it reads javascript:GoToPage(3), which seems very simple, but I can't figure out how to script it and make that page 3 show up instead of page 1. Please help, thanks in advance-


they are reloading the page via ajax.

something like that (copied from another question that i answered):

HTML

<a href="#" onclick="page('1')"></a>
<a href="#" onclick="page('2')"></a>
<a href="#" onclick="page('3')"></a>

JAVASCRIPT

function xmlhttp() {
  var x;
  try {
    x = new ActiveXObject('Microsoft.XMLHTTP');
  } catch (e) {
    try {
      x = new ActiveXObject('Msxml2.XMLHTTP');
    } catch (e) {
      try {
        x = new XMLHttpRequest();
      } catch (e) {
        x = false;
      }
    }
  }
  return x;
}

function page(idMenu) {
  var http = xmlhttp();
  if (!http) {
    alert('XmlHttpRequest non supporté');
  } else {
    var url = 'pageOutput.php?pageNo=' + idMenu;
    http.open('GET', url, true);
    http.onreadystatechange = function () {
      if (http.readyState == 4 && http.status == 200) {
        document.getElementById('pageContent').innerHTML = http.responseText;
      }
    }
    http.send();
  }
}

now all you have left to do is create a PHP where you check whatever menu ID is called and echo page content according to $_GET['pageNo']. if you already got your pages on many PHP/HTML you may also just do include and echo them...

if(isset($_GET['pageNo'])){
    //echo page code here according to $_GET['pageNo'] value
}else{
    //echo main page
}

EDIT: You may also add URL param to refer the current page so the user can reload your page from a new window without having no params loaded...

EDIT: Iframe version will just reload the whole iframe, if you look when you change page you see the iframe blink. i strongly recommand using a div content, much easyer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜