简体   繁体   English

AJAX获取相对于脚本的url

[英]AJAX getting url relative to script

How do I form the url part of open() call so it looks for the script in the correct folder without knowing beforehand where the user uploaded the script? 我如何形成open()调用的url部分,以便它在正确的文件夹中查找脚本,而无需事先知道用户在何处上载了脚本? For example lets say we have a javascript file called ajax.js and a php script called script.php. 例如,假设我们有一个名为ajax.js的JavaScript文件和一个名为script.php的php脚本。 We call open() like this: 我们这样调用open():

open("GET", "<path>/script.php", true);

Also lests say the ajax.js and script.php have been uploadeded to http://somedomain.com/scripts/ajax and the html document that imports ajax.js has the url http://somedomain.com/news/index.html . 另外,还说过ajax.js和script.php已上传到http://somedomain.com/scripts/ajax,并且导入ajax.js的html文档的URL为http://somedomain.com/news/index。 html It would be easy to say 说起来容易

open("GET", "script.php", true);

since script.php is in the same directory as ajax.js but the path is relative to the document that imported ajax.js so the browser would look for /somedomain.com/news/script.php. 由于script.php与ajax.js位于同一目录中,但是路径相对于导入ajax.js的文档而言,因此浏览器将查找/somedomain.com/news/script.php。

I need to be able to build the correct url without knowing exactly where the ajax script has be uploaded for the purpose of easy installation without having to change the url everytime. 我需要能够构建正确的url,而不必确切知道ajax脚本的上传位置,以方便安装,而不必每次都更改url。 How would I do this? 我该怎么做?

A few things to note... 注意事项...

1.) If you go to the script's URL directly and check the media type/mime is is text/javascript? 1.)如果直接转到脚本的URL并检查媒体类型/ MIME是text / javascript? If not it some browsers may not execute any scripting in the file regardless of if all the scripting is written perfectly. 否则,某些浏览器可能不会在文件中执行任何脚本,无论所有脚本编写得是否完美。

2.) You can execute PHP code in JavaScript files with JavaScript extensions (eg example.js) by using the following .htaccess command... 2.)您可以使用以下.htaccess命令在具有JavaScript扩展名(例如example.js)的JavaScript文件中执行PHP代码。

AddHandler application/x-httpd-php .js .example2 .example3
AddType text/javascript .js

If you're trying to use a dynamic path (which is commendable) you can't put an object inside of quotes otherwise it'll be treated as a string instead of an object. 如果您尝试使用动态路径(值得称赞),则不能将对象放在引号内,否则它将被视为字符串而不是对象。

var path = 'http://localhost/site1/';
alert(path+'scripts/example.js');

3.) Try to contain your scripts only to the scripts directory and only include script files inside the head element. 3.)尝试仅将脚本包含在scripts目录中,并且仅将脚本文件包含在head元素内。 This will force you to write better JavaScript. 这将迫使您编写更好的JavaScript。 For those who complain about loading scripts after content use the defer attribute... 对于那些抱怨在内容后加载脚本的人,请使用defer属性...

<head>
<script defer="defer" src="scripts/index.js" type="text/javascript"></script>
<script defer="defer" src="scripts/onload.js" type="text/javascript"></script>
</head>

4.) Merge your scripts between two files, index.js and onload.js. 4.)将脚本合并到两个文件index.js和onload.js之间。 The onload.js file should only contain an anonymous onload function and global variables (variables not inside of any kind of function)... onload.js文件应仅包含匿名的onload函数和全局变量(变量不能在任何函数内)...

var global_var = 'use these sparingly';
var path = 'http://localhost/'<?php echo 'something/'; /* dynamically make this work on localhost and example.com, use print_r($_SERVER); to get started */ ?>';

window.onload = function()
{
 function_1('parameter_1','parameter_2');
 function_2('parameter_1','parameter_2');
}

5.) You can keep multiple .js files separately and merge them in to a single index.js file using PHP so that it will still be easy to work with. 5.)您可以单独保存多个.js文件,并使用PHP将它们合并到单个index.js文件中,以便仍可以轻松使用。 If you want a very useful tool I highly recommend using Advanced Find and Replace to find and or replace scripts very quickly in your work. 如果您想要一个非常有用的工具,我强烈建议您使用“高级查找和替换”在工作中非常迅速地查找和替换脚本。

6.) Use single quotes for JavaScript and double quotes for (X)HTML. 6.)对JavaScript使用单引号,对(X)HTML使用双引号。 Consistency in how you code is a very valuable practice that pays off well once you get in to the swing of it. 编码方式的一致性是一种非常有价值的实践,一旦投入使用,它就会奏效。

7.) I highly recommend using PHP to dynamically use the $_SERVER variable to help you set the var path in JavaScript so that the exact same code will work both on localhost and on your live domains (eg example.com) without having to modify anything. 7.)我强烈建议使用PHP动态地使用$ _SERVER变量来帮助您在JavaScript中设置var路径,以便完全相同的代码在localhost和活动域(例如example.com)上都可以工作,而无需修改任何东西。 A competent programmer will always test their code in practice before committing it to a live environment, otherwise it's very easy to irk visitors because of even simple problems which can snowball because of issues like files being cached and the browser won't let go of the bad copy. 有能力的程序员在将代码提交到实际环境之前,将始终在实践中对其进行测试,否则,即使是简单的问题也可能使访问者非常烦恼,因为这些问题可能会由于文件缓存和浏览器无法释放等问题而滚雪球。不良副本。

8.) Since you're using PHP stick to using single quotes unless you really need to use double quotes. 8.)由于您使用的是PHP,因此除非您确实需要使用双引号,否则请坚持使用单引号。 If you're not aware of the difference... 如果您不知道区别...

$e = 'aaa';
echo '123$e';//outouts 123$3
echo "123$e";//outouts 123aaa

9.) You can use alert(typeof object); 9.)您可以使用alert(typeof object); to determine the context of the object (eg object (arrays are called objects in JavaScript), string, integer (number), etc. 确定对象的上下文(例如,对象(数组在JavaScript中称为对象),字符串,整数(数字)等。

10.) NEVER use simple terms for function names or variable names (eg var open = ''; or function open() {}). 10.)切勿对函数名称或变量名称使用简单的术语(例如,var open ='';或function open(){})。 There are reserved names in JavaScript and using them will cause you to pull out all of your hair in certain browsers that don't tell you that is why the script won't run (and won't trigger any errors or warnings) such as Internet Explorer. JavaScript中有保留的名称,使用它们会导致您在某些浏览器中不知所措,这就是为什么脚本无法运行(并且不会触发任何错误或警告)的原因,例如IE浏览器。 Try to use good naming schemes such as function ajax_upload(), var option_this, var option_that, etc. Editors such as SuperEdi have a functions panel, so you can group your AJAX functions and order them by name (eg ajax_1_init, ajax_2_build, ajax_3_request, ajax_4_handler, ajax_5_etc). 尝试使用良好的命名方案,例如函数ajax_upload(),var option_this,var option_that等。SuperEdi等编辑器具有功能面板,因此您可以对AJAX函数进行分组并按名称排序(例如ajax_1_init,ajax_2_build,ajax_3_request, ajax_4_handler,ajax_5_etc)。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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