Javascript Ajax,添加GET var onload
我想,当我加载一个网页时,加入获取屏幕的宽度和高度,这样我的页面就可以使用这些变量重定向到一个特定的页面 我已经寻找了函数,似乎应该使用window.screen.width和window.screen.height 另外,我想我应该使用$.get来发送变量 所以我提出了这个脚本,但我假设我混合了JS和AJAX,如果可以的话,我不知道。最重要的是,我不知道在加载页面时如何运行该函数Javascript Ajax,添加GET var onload,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我想,当我加载一个网页时,加入获取屏幕的宽度和高度,这样我的页面就可以使用这些变量重定向到一个特定的页面 我已经寻找了函数,似乎应该使用window.screen.width和window.screen.height 另外,我想我应该使用$.get来发送变量 所以我提出了这个脚本,但我假设我混合了JS和AJAX,如果可以的话,我不知道。最重要的是,我不知道在加载页面时如何运行该函数 $(document).ready(function() { ratio(); function
$(document).ready(function() {
ratio();
function ratio() {
var Vorientation;
if(window.screen.width>window.screen.height)
{
Vorientation = 1;
}
else
{
Vorientation = 2;
}
$.get( "index.php?orientation="+Vorientation );
alert("ok");
}
});
以下是html:
<body>
<?php
if(isset($_GET['orientation']))
{
echo"ok";
}
?>
</body>
要在加载页面时运行该函数,必须将代码包装在$document.readyfunction{};功能 使用以下命令:
$(document).ready(function(){
window.Vorientation=0;
ratio();
function ratio() {
if(window.screen.width>window.screen.height)
{
Vorientation = 1;
}
else
{
Vorientation = 2;
}
$.get( "index.php", { orientation: Vorientation} )
.done(function( data ) {
alert( "Data Loaded: " + data );
});
}
});
要在加载页面时运行该函数,必须将代码包装在$document.readyfunction{};功能 使用以下命令:
$(document).ready(function(){
window.Vorientation=0;
ratio();
function ratio() {
if(window.screen.width>window.screen.height)
{
Vorientation = 1;
}
else
{
Vorientation = 2;
}
$.get( "index.php", { orientation: Vorientation} )
.done(function( data ) {
alert( "Data Loaded: " + data );
});
}
});
您只需要将jQuery添加到脚本中,并将代码添加到DocumentReady函数中
您只需要将jQuery添加到脚本中,并将代码添加到DocumentReady函数中
混合js和ajax:ajax不是一种语言,它是一种从服务器检索响应的方法,例如从带有javascript的PHP代码中检索响应,通常用于根据用户输入生成页面内容。$。get是jQuery,它是执行ajax的js库GETrequest@asdf_enel_hak实际上,OP使用它的方式,它是一个全局变量。你是对的;它应该在if-like var方向之前声明;混合js和ajax:ajax不是一种语言,它是一种从服务器检索响应的方法,例如从带有javascript的PHP代码中检索响应,通常用于根据用户输入生成页面内容。$。get是jQuery,它是执行ajax的js库GETrequest@asdf_enel_hak实际上,OP使用它的方式,它是一个全局变量。你是对的;它应该在if-like var方向之前声明;我认为漩涡方向需要在if/else no之外声明?它首先需要声明。在这里使用global是不对的,我认为需要在if/else no之外声明Vorientation?首先需要声明它。在这里使用全局函数是不对的,Ajax请求发生在后台。它们不会更改浏览器地址栏中的URL;他们只是将响应数据返回到您的回调函数数据中。您是否询问如何在服务器端获得VorOrientation的值?您能在ajax之前先console.LogVorOrientation吗?确保这不是问题所在。我不知道你如何处理你的服务器:@Ezhno,你需要使用这个:$\u POST['orientation'],而不是$\u POST['vorientiation'],因为你发送这个:{orientation:vorientiation}如果你的PHP从查询字符串中获取值,试试$.get index.PHP?orientation=+vorientiation$_POST无法处理此客户端代码,因为您正在发送GET请求。AJAX请求发生在后台。它们不会更改浏览器地址栏中的URL;他们只是将响应数据返回到您的回调函数数据中。您是否询问如何在服务器端获得VorOrientation的值?您能在ajax之前先console.LogVorOrientation吗?确保这不是问题所在。我不知道你如何处理你的服务器:@Ezhno,你需要使用这个:$\u POST['orientation'],而不是$\u POST['vorientiation'],因为你发送这个:{orientation:vorientiation}如果你的PHP从查询字符串中获取值,试试$.get index.PHP?orientation=+vorientiation$_POST无法处理此客户端代码,因为您正在发送GET请求。
$(function() {
ratio();
function ratio() {
var Vorientation;
if(window.screen.width>window.screen.height) {
Vorientation = 1;
} else {
Vorientation = 2;
}
$.get("index.php", { orientation: Vorientation})
.done(function(data) {
alert("Data Loaded: " + data);
});
}
});