I am a newbie to jQuery / javascript and I had this working without checking the window size first. Then messed around with it and can not get it to work. The redirect is supposed to replace mysite.com/index.php?querystring
with mysite.com/mobile.php?querystring
if screen size is less then 699. Please help. Thank You.
我是一个jQuery / javascript的新手,我没有先检查窗口大小就完成了这个工作。然后把它弄得一团糟,无法让它工作。重定向应该替代mysite.com/index.php?变量的名称与mysite.com/mobile.php?如果屏幕尺寸小于699,查询字符串。请帮助。谢谢你!
This function seems to work exaclty how I need it but need to have onload with if screen size is less then.
这个函数看起来很精确,我需要它,但是如果屏幕尺寸变小,就需要加载。
$('a').each(function(index, a) {
var href = $(a).attr('href');
$(a).attr('href', 'http://mysite.com/mobile.php?redirect=' + href;)
}
}
}
//below is not working
/ /下面是不工作
function checkWidth() {
var windowSize = $(window).width();
if (windowSize <= 699) {
window.onload = function() {
/* onload code */
// Execute on load
//checkWidth();{
var anchors = document.getElementsByTagName("a");
for (var i = 0; i < anchors.length; i++) {
anchors[i].href = "http://mysite.com/mobile.php?redirect=" + anchors[i].href
/* function checkWidth() {
var windowSize = $(window).width();*/
}
}
2 个解决方案
#1
2
If you intend on using jQuery, this should work:
如果您打算使用jQuery,那么应该这样做:
$(document).ready(function() {
var window_width = $(window).width();
if( window_width < 699 ) {
$('a').each(function(index, a) {
var href = $(a).attr('href');
$(a).attr('href', 'http://mysite.com/mobile.php?redirect=' + href;
});
}
});
#2
1
This is really something you should be doing server-side. Because someone isn't exactly going to be switching the device over the course of the session, you should check the device when they first visit the site, and then create a session variable storing it. Then, on every new page have the server check the variable and use it to determine which links to put in. If you're content with doing it client-side, though, Ryan Pilbeam's answer should work.
这是您应该做的服务器端操作。因为在会话过程中,没有人确切地要切换设备,所以当他们第一次访问该站点时,您应该检查设备,然后创建一个存储它的会话变量。然后,在每个新页面上都有服务器检查变量并使用它来确定要输入的链接。不过,如果你满足于做客户端,那么Ryan Pilbeam的答案应该是可行的。
#1
2
If you intend on using jQuery, this should work:
如果您打算使用jQuery,那么应该这样做:
$(document).ready(function() {
var window_width = $(window).width();
if( window_width < 699 ) {
$('a').each(function(index, a) {
var href = $(a).attr('href');
$(a).attr('href', 'http://mysite.com/mobile.php?redirect=' + href;
});
}
});
#2
1
This is really something you should be doing server-side. Because someone isn't exactly going to be switching the device over the course of the session, you should check the device when they first visit the site, and then create a session variable storing it. Then, on every new page have the server check the variable and use it to determine which links to put in. If you're content with doing it client-side, though, Ryan Pilbeam's answer should work.
这是您应该做的服务器端操作。因为在会话过程中,没有人确切地要切换设备,所以当他们第一次访问该站点时,您应该检查设备,然后创建一个存储它的会话变量。然后,在每个新页面上都有服务器检查变量并使用它来确定要输入的链接。不过,如果你满足于做客户端,那么Ryan Pilbeam的答案应该是可行的。