2012-01-27 194 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/

相關問題