While coding my personal website, I ran into a problem: I want to create a modal window with an “iframe” tag, but Google Translate does not support text in the iframe. So I tried to embed another HTML file in index.html with JavaScript or PHP.
GOAL
The goal today is to embed sub.html in index.html without using iframe tags so that Google Translate will work for the embedded text.
Below is how to embed the code in an iframe tag.
<iframe src="sub.html"></iframe>
Environment
PHP8.3.8 Latest Google Chrome (Version 127.0.6533.90)
<?php include 'sub.html'; ?>
//<?php include('sub.html'); ?> also works
Change index.html into index.php to use PHP function in index file. Or you can use html as PHP by using “AddType” in .htaccess. Reference: Parse HTML as PHP using HTACCESS File
<div id="frame-content"></div>
<!-- need to read jQuery -->
<script src="jquery.min.js"></script>
<script>
$("#frame-content").load('sub.html');
</script>
Trouble Shooting
Opening index.html in a local environment causes a CROS policy error as below. For correct results, we must use a web server or a local HTTP server.
Access to XMLHttpRequest at 'file:///C:/.../sub.html' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, isolated-app, chrome-extension, chrome, https, chrome-untrusted.
Today’s goal is to implement a search suggestion as below. The suggestion should be displayed without page transition while inputting the search word.
Environment
Windows10 XAMPP 7.4.10
What is Ajax
AJAX, Asynchronous JavaScript And XML, is a term that refers to a group of Web development techniques for asynchronous communication on the client side using JavaScript. Please check “What Is AJAX (and Asynchronous Communication)?” for details about ajax.
The following is a brief explanation about the ajax mechanism.
How Ajax works
In Ajax, the 2 system “sending the request and getting the response from the server” and “get the data from the browser and reflect the received data to the browser” are separated. So users can operate on the browser while waiting the response.
JSON is used for data exchange in PHP. But how can I create json data in php and pass it to JavaScript?
GOAL
Today’s goal is to generate Json data from PHP array and get the data in JavaScript. This can be used when getting data from the server in PHP and processing the data in JavaScript.