2012-01-27 123 views
1

由于某些原因,这个基本的JS检测浏览器的分辨率适用于Safari和Chrome,但不适用于IE9或FF9。基本上,无论何时这个表单都将被提交,JS应该用浏览器的高度和宽度更新隐藏字段。但是这再次在IE9或FF9中不起作用。JS检测浏览器分辨率在Chrome浏览器和Safari浏览器,但不是IE9或FF9

提交按钮 -

<input type="image" src="lib/send_feedback.jpg" border="0" class="feedback-submit-img" onClick="javascript: validate(); return false;"/> 

隐藏表单代码 -

<input name="h" id="h" type="hidden" value="" /><input name="w" id="w" type="hidden" value="" /> 

相关的jQuery -

// Submit form to next page 
function submitForm() { 
// document.forms["feedbackform"].submit(); 
    document.feedbackform.submit(); 
} 
// Submit form and validate email using RFC 2822 standard 
function validateEmail(email) { 
    // Modified version original from: http://stackoverflow.com/a/46181/11236 
    var re = /^(([^<>()[\]\\.,;:\[email protected]\"]+(\.[^<>()[\]\\.,;:\[email protected]\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; 
    return re.test(email); 
} 
// Return true if email field is left unchanged 
function originalText(email){ 
    var defaultMsg; 
    defaultMsg = "Enter your email address (optional)"; 
    if(defaultMsg == email){ 
     return true;  
    } 
    return false; 
} 
// Verify or decline with error message 
function validate(){ 
    $("#result").text(""); 
    var email = $("#email").val(); 
    if ((validateEmail(email)) || originalText(email)) { 
     w.value = screen.width; 
     h.value = screen.height; 
     submitForm(); 
    } else { 
     $("#result").text(email + " is not a valid email."); 
     $("#result").css("color", "red"); 
    } 
    return false; 
} 
$("form").bind("submit", validate); 

Here is the entire codethe CSS

+0

什么意思是“不起作用”?你有错误吗?如果是这样,在哪里? –

回答

2

的jQuery有一种方法可以获得符合跨浏览器的高度(使用.height()方法)。手动的方式做跨浏览器兼容的文件的高度是后话了以下

function getDocHeight() { 
    var D = document; 
    return Math.max(
     Math.max(D.body.scrollHeight, D.documentElement.scrollHeight), 
     Math.max(D.body.offsetHeight, D.documentElement.offsetHeight), 
     Math.max(D.body.clientHeight, D.documentElement.clientHeight) 
    ); 
} 

http://james.padolsey.com/javascript/get-document-height-cross-browser/

,这里是(在同一链路从评论)jQuery的版本

$.getDocHeight = function(){ 
    return Math.max(
     $(document).height(), 
     $(window).height(), 
     /* For opera: */ 
     document.documentElement.clientHeight 
    ); 
}; 
0

当你说你的浏览器分辨率是指显示器的物理尺寸?如果是这样,您可以使用:

var w = window.screen.width; 
var h = window.screen.height; 

这些与设备大小相同。

请参阅:responsejs.com/labs/dimensions/

相关问题