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:
JavaScript
x
22
22
1
$(init);
2
function init(){
3
$(function() {
4
$("#tabs").tabs();
5
});
6
7
$('#testURLs a').on('click', function (event) {
8
event.preventDefault();
9
10
// My JQuery tabs: 0: Search; 1: Documents
11
$( "#tabs" ).tabs({ active: 1 });
12
$.ajax({
13
method: "GET",
14
// url: "http://127.0.0.1:8080/test/d1.html",
15
url: this.href,
16
data: {}
17
}).done(function(response) {
18
$("#documents_tab_docs").html(response);
19
});
20
})
21
}
22
Advertisement
Answer
I managed to engineer a solution. For those interested, here are the salient portions of the code.
Ajax
JavaScript
1
28
28
1
// ...
2
// Localserver: http-server --cors /mnt/Vancouver/
3
//...
4
var output = '<div id="title"><h3>
5
<a class="docTitle" href="http://127.0.0.1:8081/programming/datasci/solr/test/'
6
+ doc.filename + '"><b>' + doc.title + '</b></a></h3>';
7
// ...
8
return output;
9
//...
10
//----------------------------------------
11
//...
12
init: function () {
13
$(document).on('click', 'a.docTitle', function () {
14
var $this = $(this);
15
var url = this.href;
16
17
$.ajax({
18
method: "GET"
19
}).done(function(response) {
20
// Use numbered (not named) tabs:
21
$( "#tabs" ).tabs({ active: 1 });
22
$("#iframe_docs").attr("src", url);
23
});
24
25
return false;
26
});
27
}
28
HTML
JavaScript
1
13
13
1
<!-- -->
2
<div id="documents_tab" class="tab">
3
<!-- <h2>Documents</h2> -->
4
<ul>
5
<!-- Documents, etc. from the Search tab will appear here. -->
6
<!-- https://stackoverflow.com/questions/40903065/how-to-embed-iframe-with-external-website -->
7
<div id="documents_tab_docs"></div>
8
<iframe id="iframe_docs" src="">
9
</iframe>
10
</ul>
11
</div>
12
<!-- -->
13
CSS
JavaScript
1
11
11
1
#iframe_docs {
2
border-style: none;
3
width: 100%;
4
/* height: 100%; */
5
/* vh : viewport height */
6
/* https://stackoverflow.com/questions/5272519/how-do-you-give-iframe-100-height */
7
/* https://stackoverflow.com/questions/5867985/full-screen-iframe-with-a-height-of-100 */
8
height: 100vh;
9
background: #f8f9f9;
10
}
11
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, …: