I would like to embed the HTML page within the specified div element

When attempting to load a HTML page inside the div, I encountered the following issue:

<a href="#1" title="" class="base-banner" page="www.google.com for example">

<img src="images" alt=""></a> <div id="landingpage"> </div>

Here is the JavaScript code being used:

$(document).ready(function() {
            $(".base-banner").on("click", function(){  
                $("#landingpage").show().load($(this).attr("page"));

                return false;
            });
        });

The loading works properly when trying to load a local page, but fails when attempting to load a live page. Any insights on where I went wrong and what needs to be corrected would be greatly appreciated.

Thank you in advance for your help.

Answer №1

It is not possible to query pages across domains due to AJAX policy restrictions.

However, a workaround solution can be implemented using the following code snippet:

<div id="siteloader"></div>


$.ajaxSetup({
    scriptCharset: "utf-8", //possibly "ISO-8859-1"
    contentType: "application/json; charset=utf-8"
});

$.getJSON('http://whateverorigin.org/get?url=' + 
    encodeURIComponent('http://google.com') + '&callback=?',
    function(data) {
        console.log("> ", data);
        //If the expected response is text/plain
        $("#target").html(data.contents);
        //If the expected response is JSON
        //var response = $.parseJSON(data.contents);
        //console.log("> ", response);
});

http://jsfiddle.net/SsJsL/2011/

Answer №2

If you're trying to embed an html file within a div, there are a few ways to achieve this – one option is using Ajax or iframe. Below is a sample code snippet for loading the content using an iframe:

function load_home (e) {
    (e || window.event).preventDefault();
    var con = document.getElementById('content')
    ,   xhr = new XMLHttpRequest();

   xhr.onreadystatechange = function (e) { 
    if (xhr.readyState == 4 && xhr.status == 200) {
      con.innerHTML = xhr.responseText;
    }
   }

xhr.open("GET", "http://www.yoursite.com/home.html", true);
xhr.setRequestHeader('Content-type', 'text/html');
xhr.send();
}

Answer №3

This question may be old, but for those facing the same issue, I discovered that if you haven't opened the page in your browser first, nothing will display when trying to load the file into your div.

To solve this, simply open the page in your browser by typing the page's name (e.g., "http://localhost/myPage.html"). If it displays correctly, then it will load inside your div.

I'm not entirely sure why this is necessary, perhaps it has to do with loading the page onto the server or something similar.

I hope this explanation helps.

Answer №4

Due to the CORS policy, content from pages on different domains may be restricted by the browser.

To bypass this restriction, you can use a third-party proxy like .

To implement this, simply add the cors-anywhere link before your URL as shown below:

$(document).ready(function() {
  $(".base-banner").on("click", function() {
    // Use the cors-anywhere link provided below.
    $("#landingpage").show().load('https://cors-anywhere.herokuapp.com/' + $(this).attr("page"));

    return false;
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a href="#1" title="" class="base-banner" page="https://www.google.com">

  <img src="images" alt=""> click here </a>
<div id="landingpage"> </div>

Check out this Demo Link.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Optimal arrangement for z-index and opacity

In my design, I have placed a nested link within two absolutely positioned divs structured like this: <div class="container"> <div class="leftPostHolder"> <div class="leftPost"> <h3><a href="#">link ...

What is the best way to trigger a jQuery function when an option is selected from a Select2 dropdown menu?

I have implemented Select2 for custom dropdown styling. The options are structured like this: <option value="001,100">Option 001</option> <option value="002,200">Option 002</option> <option value="003,300">Option 003</opti ...

Implementing jQuery into cfdiv component

My current setup involves using a cfdiv to bind data from another page, like (bind.cfm?id=123). This other page (bind.cfm) contains a jQuery scrollable feature from Flowplayer found at . However, the scrolling function is not working as expected. I have al ...

Conceal information from view without hiding pseudo content visually

Here's a method I'm using to incorporate icons through the use of :before or :after pseudo content: <span class="icon icon__example is-content-visually-hidden">assistive text</span> Is there a way to hide the assistive text visually ...

Unexpected CSS counter reset issue encountered within nested elements

Utilizing css counters for the formatting of certain wiki pages is a common practice that I implement by adding CSS directly to the wiki page. One particular use case involves numbering headers using counters. Below is a snippet of the code that demonstra ...

Ways to remove border when image is not present

I've been attempting to use JavaScript to hide the border, but it doesn't seem to be working. Is it possible to hide the border in HTML using Java? ......................... Check out my page here Here is a snippet of my HTML: <!-- Single P ...

Incorporate JavaScript values into an HTML form to submit them to PHP

How can I successfully POST the values of 'vkey' and 'gene+varient' when submitting a form in HTML? The values are retrieved using JS code and displayed correctly on the form, but are not being sent upon submission. <form action="ac ...

Can you provide instructions on how to use the jquery ajax post method to upload data to a MYSQL database

While I am familiar with performing a regular PHP post method where you specify the $conn variable containing the IP address and login details for the database, I find myself uncertain about how to achieve the same using jQuery AJAX post method. $.post( ur ...

React not displaying wrapped div

I am facing an issue with my render() function where the outer div is not rendering, but the inner ReactComponent is displaying. Here is a snippet of my code: return( <div style={{background: "black"}}> <[ReactComponent]> ...

How can you verify the validity of data in Laravel datatables?

When attempting to retrieve data from another table through a foreign key, I need to validate whether the data is empty or not. For example: if the foreign key data is empty, I'd like to return null. I've tried various methods to achieve this, ...

Why is the outline buttons class only displaying gray colors for me?

I'm having trouble with the outline buttons class (btn btn-outline-primary) in Bootstrap. For some reason, all the buttons appear gray instead of colored as they should. Here's an example of how the buttons should look: https://ibb.co/ChKf83y, bu ...

Loading necessary CSS when needed in React JS

I am currently in the process of converting a bootstrap template to react framework. My question is, is there a way for me to load stylesheets on demand? For instance, if I have 2 components and I import the same stylesheet separately in both components, ...

Is there a way to send a variable to the alert function using $.ajax()?

I need assistance with confirming the deletion of a record. When the user clicks on the button, it should send the value 'Yes' to a $_POST request in PHP for deleting the record. However, instead of working as expected, it is showing me the JavaS ...

What is the method for adjusting the text color in an HTML document?

Is there a way to modify the text color of this statement: You Have Already Earned Credit For This Link? <div id=\"myform\" style=\"visibility: hidden;\"> <font size=2><b>You Have Already Earned ...

Incorporating a variety of images into this hexagonal design framework

Hello there! I am just starting to learn CSS and HTML, and I have a question. Is it possible for me to use the same CSS styling but have different images inside each of the hexagons? I want to replace the image '' with multiple images without rep ...

Unlock the Power of jQuery Plugins: Implementing Global Settings Modifications

I have developed a jQuery plugin ( https://github.com/OscarGodson/jKey ) and some users are requesting localization support. My initial idea was to add an additional parameter in the plugin for localization, such as: $(window).jkey('?',callback, ...

Using jQuery to import certain library causes the XPage to completely lock up

My XPage only relies on 2 js imports - one being JQuery and the other dependent on JQuery specifically for smart companies search in the Russian Federation. However, a challenge arises when trying to import the second library as it causes the entire page t ...

Exporting an HTML table to Excel while excluding any hidden <td> elements

I have a web application with an HTML table that displays data to users. I wanted to export the table to Excel and found a jQuery code for it. However, the exported data includes information that is hidden in the table. Is there a way to export the tabl ...

Choosing Tags with Ajax in Select2

The JSON data below is fetched from /tags: [ { "id": "CSS", "text": "CSS" }, { "id": "HTML", "text": "HTML" }, { "id": "JavaScript", "text": "JavaScript" }, { "id": "jQuer ...

How can you extract a JSON object from a JSON array based on a key that is associated with a specific JSON

Is it possible to retrieve a specific JSON object from an array of JSON objects based on a condition? [ { id: 1, name: "larry" }, { id: 2, name: "curly" }, { id: 3, name: "moe" } ] For example, if I want to extract the object where name is equal to " ...