简体   繁体   中英

Javascript ajax scripts in <head>?

I have 2 html page (main and details): the main page consists of a table and a empty div. When the user clicks one a table row, the empty div is filled via AJAX from another page (details page).

On the details page I want to load a Google Map. Also I would like the page to be operational by itself (standalone), not just via AJAX.

So here is my problem: To use Google maps I have to include this script in head of html:

<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>

If I include this in the details page, it works fine standalone. But it doesn't work when I try to get it via AJAX from main page. Google server hangs, and doesn't progress.

On the other hand, if I include it in the main page, AJAX works fine, but the details page is not operational on its own, since its missing a vital include.

I'd really like to leave it in the details page, since it has much more logic to be there. Is there any way I can load the script in the main page, from the details page?

Generally what is the best approach with javascript including and AJAX? Keep everything in main page? Or is there any mechanism to load everything into main page, but keep the code in ajaxed pages?

Btw. I'm using jQuery, but it is not really important. This is a design issues, not a library problem.

Since you are not using IFRAME , it is best that you include the JAVASCRIPT in the main page rather than detail page - since you can do and the js will work. This I think will fix the ajax issue for you and the script is loaded once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM