[英]How do you load() with jQuery the way you would include() with PHP inline code like this?
UPDATE AT BOTTOM I am used to using a selector that involves an id or class, so not even sure this is possible at least the function I am thinking. 在底部进行更新我习惯于使用包含ID或类的选择器,因此,至少在我所考虑的功能上,甚至不确定这样做是否可行。 I have a separate file called replist.html that I hold data in a one-to-one relationship like this (partial list of 35,000 lines)...
我有一个名为replist.html的单独文件,它以这种一对一的关系保存数据(部分列表35,000行)...
'Illinois': 'joesmoe',
'Nevada': 'messytess',
<!-- start of new zip codes -->
'01001': 'julsguls',
'01002': 'julsguls',
I initially pulled the file into page I am using script on with an include_once(), but now am working on modifications to alter for Mobile devices and their handling of location detection (I am converting IP to zip). 我最初将文件拉到页面上,正在使用include_once()使用脚本,但现在正在进行修改以更改移动设备及其对位置检测的处理(我将IP转换为zip)。 Because of the nature, I am looking to access data through client-side script over the PHP.
由于其性质,我希望通过PHP通过客户端脚本访问数据。 Here is what I have so far...
这是我到目前为止所拥有的...
<script>
...
<?php
if(!(($detect->isMobile()))) { ?>
var reps = {
<?php include_once('repList.html'); ?>
};
<?php
}
else { ?>
<!-- custom mobile location detection here -->
reps = {***would like to get load() here***};
<?php } ?>
...
</script>
...the else I am looking to add mobile location detection script, then load() my repList.html file. ...否则,我要添加移动位置检测脚本,然后将repList.html文件load()加载 。
I have simplified the else to just focus on how to get my load() in there. 我已经简化了else,只专注于如何将load()放入其中。 If anyone has a better jQuery, other JavaScript library or any client side solution I am open.
如果有人拥有更好的jQuery,其他JavaScript库或任何客户端解决方案,我就会开放。 The script I plan to use pre-this process is client side and why I can not use PHP as I was using.
我计划在此过程之前使用的脚本是客户端,为什么我不能像以前那样使用PHP。
I believe I will keep the PHP and go with... 我相信我会保留PHP并继续使用...
var reps_load = {<?php include_once('repList.html'); ?>};
<?php
if(!(($detect->isMobile()))) { ?>
var reps = reps_load;
<?php
}
else {
...
According to the manual load()
does: 根据手动
load()
可以:
Load data from the server and place the returned HTML into the matched element.
从服务器加载数据,并将返回的HTML放入匹配的元素。
So that cannot assign anything to your javascript variable. 这样就无法为您的javascript变量分配任何内容。
You could json_encode
the data server-side and use jQuery.getJSON()
instead, assigning the returned value to your variable in the callback function. 您可以在数据服务器端
json_encode
并使用jQuery.getJSON()
代替,将返回的值分配给回调函数中的变量。
But you would call another php file that in turn reads your data and prepares it. 但是您将调用另一个php文件,该文件依次读取您的数据并进行准备。
Something like: 就像是:
else
{
?>
<!-- custom mobile location detection here -->
var reps;
$.getJSON('script_that_returns_data_as_json.php', function(data) {
reps = data;
// you might need to process it to get it the form you want...
});
<?php
}
?>
This method uses only plain javascript, but it works even across sites in case your data is on a different domain. 此方法仅使用普通的javascript,但即使您的数据位于其他域中,它也可以跨站点使用。 You can prepend a file with the data included as JSON or javascript variables
您可以在文件前面加上包含为JSON或javascript变量的数据
var script=document.createElement("script");
script.src="your_url.js"
var head = document.getElementsByTagName('head')[0]
head.insertBefore(script,head.firstChild)
and then your data file could look something like: 然后您的数据文件可能类似于:
items=[
{"state":"Illinois","name":"joesmoe"},
{"zip":"01001", "value":"julsquls"}
];
which can be accessed by: 可以通过以下方式访问:
for (var i=0;i<items.length;i++){
if(items[i].state) /*state stuff*/;
if(items[i].zip) /*zip stuff*/;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.