简体   繁体   中英

Javascript redirects to source code instead of loading

I'm trying to generate an Amazon ad that should look like this.

<script type="text/javascript"><!--
amazon_ad_tag = "xxxxxxx"; amazon_ad_width = "160"; amazon_ad_height = "600";//--></script>
<script type="text/javascript" src="http://www.assoc-amazon.com/s/ads.js"></script>

Below are parts of my script that should achieve that. The script BTW is for rotating more types of affiliate ads.

First is a global variable with all the Amazon ad options...

ad_code = 'amazon_ad_tag = "xxxxxxx"; amazon_ad_width = "160"; amazon_ad_height = "600";';

Next I'm loading the ad_code in a "ad_slot" div. The contents of ad_code should appear between <script> tags.

$('.amazon_ad').html('<scr'+'ipt type=\"text/javascript\"><!--' + ad_code + '//--></scr'+'ipt><scr'+'ipt src=\"' + window.location.protocol +'//www.assoc-amazon.com/s/ads.js\" type=\"text/javascr'+'ipt\"></scr'+'ipt>');

The result should be the first code I posted and display an Amazon banner, instead my browser goes to http://www.assoc-amazon.com/s/ads.js , the script that should load.

Does anyone know what I'm doing wrong?

是否将它包装在$(document).ready(function() {...中?如果它位于其中,对我来说很好用。

Ensure the content/mime type for the .js files is set correctly in your server configuration. You can do this with .htaccess files.

Also you need to comment out or remove the <!-- comment in your script tag as that is not valid js

This is probably not the best time to criticize your code but:

All your script should be included externally, not inline. Do not use , this is a deprecated, ancient way of including inline script. If you must, use // I see in your script you are using document.write(). DON'T!

You cannot cheat and include a script in such a way and expect it to be executed.

Use jQuery.getScript()

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