Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

javascript - Passing an Ajax-generated URL to a JQuery tab in web page

I am encountering an issue in processing Ajax-encoded URLs.

I am querying a database (Solr) via an Ajax script, sending the output to a web page (served locally only on a localhost webserver on my home computer).

When I click Ajax-generated links (URLs), they open in another browser tab, not the source web page.

For prototyping, hard-coded URLs manually added to my web page display properly, opening in the same web page in a JQuery "Documents" tab:

$(init);
function init(){
  $(function() {
    $("#tabs").tabs();
  });

  $('#testURLs a').on('click', function (event) {
    event.preventDefault();

    // My JQuery tabs: 0: Search; 1: Documents 
    $( "#tabs" ).tabs({ active: 1 });
    $.ajax({
      method: "GET",
      // url: "http://127.0.0.1:8080/test/d1.html",
      url: this.href,
      data: {}
      }).done(function(response) {
          $("#documents_tab_docs").html(response);
        });
  })
}
question from:https://stackoverflow.com/questions/65855113/passing-an-ajax-generated-url-to-a-jquery-tab-in-web-page

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I managed to engineer a solution. For those interested, here are the salient portions of the code.

Ajax

// ...
// Localserver: http-server --cors /mnt/Vancouver/
//...
var output = '<div id="title"><h3>
    <a class="docTitle" href="http://127.0.0.1:8081/programming/datasci/solr/test/'
    + doc.filename + '"><b>' + doc.title + '</b></a></h3>';
// ...
return output;
//...
//----------------------------------------
//...
init: function () {
  $(document).on('click', 'a.docTitle', function () {
      var $this = $(this);
      var url = this.href;

      $.ajax({
      method: "GET"
      }).done(function(response) {
          // Use numbered (not named) tabs: 
          $( "#tabs" ).tabs({ active: 1 });
          $("#iframe_docs").attr("src", url);
          });

      return false;
  });
}

HTML

<!-- ... -->
<div id="documents_tab" class="tab">
  <!-- <h2>Documents</h2> -->
  <ul>
    <!-- Documents, etc. from the Search tab will appear here. -->
    <!-- https://stackoverflow.com/questions/40903065/how-to-embed-iframe-with-external-website -->
    <div id="documents_tab_docs"></div>
      <iframe id="iframe_docs" src="">
      </iframe>
  </ul>
</div>
<!-- ... -->

CSS

#iframe_docs {
  border-style: none;
  width: 100%;
  /* height: 100%; */
  /* vh : viewport height */
  /*   https://stackoverflow.com/questions/5272519/how-do-you-give-iframe-100-height */
  /*   https://stackoverflow.com/questions/5867985/full-screen-iframe-with-a-height-of-100 */
  height: 100vh;
  background: #f8f9f9;
}

Here is a video of that implementation (note: dummy data; raw, development code):

https://www.youtube.com/watch?v=sLL9ooqi_xU

Relevant background here (mine), re: JQuery tabs, ...:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...