Javascript是否与PHP中的__FILE__变量类似? [英] Is there any analogue in Javascript to the __FILE__ variable in PHP?
本文介绍了Javascript是否与PHP中的__FILE__变量类似?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
非常简单,我有一个JS脚本,该脚本包含在许多不同的站点中,并且需要将参数传递给它.
Pretty straightforward, I've got a JS script that's included on many different sites and needs to have parameters passed to it.
如果可以通过URL传递这些信息,将非常有用,例如:
It would be useful if those could be passed via the URL, eg:
<script type="text/javascript"
src="http://path.to/script.js?var1=something&var2=somethingelse"></script>
是的,您仍然可以在单独的脚本标签中预填充变量,但这是一个杂乱无章的代码,传递起来不太容易:
Yes, you can still pre-populate variables, in a separate script tag, but it's a smidge messy and less easy to pass around:
<script type="text/javascript">var1=something; var2=somethingelse</script>
<script type="text/javascript" src="http://path.to/script.js"></script>
推荐答案
error.fileName
会告诉您脚本的来源文件(不确定它是否在每个浏览器中都有效;我已经在Firefox和Opera中对其进行了测试)
error.fileName
will tell you the file that a script is from (not sure if it works in every browser; I've tested it in Firefox & Opera)
var filename = (new Error).fileName;
这篇关于Javascript是否与PHP中的__FILE__变量类似?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文