This question already has answers here:
Browser detection in JavaScript? [duplicate]
(25 answers)
Closed 7 years ago.
I want to modify css of an element with jQuery only if the user agent is IE9 or later. How can I do this? This is my code:
var lancetta = $(document).find('.seconds');
if(msie version is >= 9){
lancetta.css({'-ms-transform' : 'rotateZ(360deg)'});
}else{
lancetta.css({'-webkit-transform' : 'rotateZ(360deg)',
'-moz-transform' : 'rotateZ(360deg)',
'transform' : 'rotateZ(360deg)'});
}
You can try this:
var IE9 = document.all && !window.atob;
if (IE9)
{
//IE9+ only
}
else
{
//rest
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
The below Javascript (triggering the pagination background color of an element, when another element scrolls into view) doesn't work in Internet Explorer but in all other browsers. Does anyone have any idea why?
The code basis is also available in my pen: https://codepen.io/headstarterz/pen/PMdZdV/
<script>
function inViewport(element) {
// Get the elements position relative to the viewport
var bb = element.getBoundingClientRect();
// Check if the element is outside the viewport
// Then invert the returned value because you want to know the opposite
return !(bb.top > innerHeight || bb.bottom < 0);
}
var project1 = document.querySelector(".project-trigger1");
var project2 = document.querySelector(".project-trigger2");
var project3 = document.querySelector(".project-trigger3");
var pagination1 = document.querySelector(".bullet1");
var pagination2 = document.querySelector(".bullet2");
var pagination3 = document.querySelector(".bullet3");
// Listen for the scroll event
document.addEventListener("scroll", event => {
// Check the viewport status
if (inViewport(project1)) {
pagination1.style.background = "#e3e3e3";
} else {
pagination1.style.background = "transparent";
}
});
document.addEventListener("scroll", event => {
// Check the viewport status
if (inViewport(project2)) {
pagination2.style.background = "#e3e3e3";
} else {
pagination2.style.background = "transparent";
}
});
document.addEventListener("scroll", event => {
// Check the viewport status
if (inViewport(project3)) {
pagination3.style.background = "#e3e3e3";
} else {
pagination3.style.background = "transparent";
}
});
</script>
Script works in Chrome, Safari, Firefox, Edge
Script doesn't work in Internet Explorer 11
It is probably two reasons:
Firstly your closing <script> tag is spelled wrong (<skript>).
Also, you're using 'fat arrow functions', which is a feature of ES6. I would look into something like Babel to transpile your code to a I.E. friendly syntax
This question already has answers here:
Check if a popup window is closed
(5 answers)
Closed 7 years ago.
I need to attach some event to my popup while my window is being closed and I can't figure out how to do this.
$scope.loginWithOk = function() {
var left, popup, top;
$window.$scope = $scope;
left = screen.width / 2 - 250;
top = screen.height / 2 - 250;
popup = $window.open('/auth/odnoklassniki', '', 'top=' + top + ',left=' + left + ',width=700,height=500');
};
How can I do this?
When I go with
popup.onclose = function() {
return alert(2222222222);
};
I can't see any alert so this option doesn't seem to be working.
You can try using window.onunload
https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onunload
More details as requested:
popup.onunload = function() {
return alert(2222222222);
}
This question already has answers here:
What are alternatives to document.write?
(11 answers)
Closed 8 years ago.
So I have a script that generates a hex code, and changes the background. Here's the code
$(document).ready(function() {
$(document).keydown(function(e) {
if (e.keyCode == '32') {
var color = "#" + Math.random().toString(16).slice(2, 8);
document.write(color);
document.body.style.backgroundColor = color;
}
});
});
The issue is when I press space, it only changes the color once, and I won't be able to press space again to generate another color without reloading the page. Here's a demo. Any ideas?
Do not use document.write.
it will wipe your script so you should append it in body:
$(document.body).append(color);
document.body.style.backgroundColor = color;
or add a span and set its text:
$("span").text(color);
UPDATED JSBIN
http://jsfiddle.net/EVjaH/
$(document).ready(function(){
$('body').on('keydown',function(e) {
if (e.which == '32') {
var color = "#" + Math.random().toString(16).slice(2, 8);
$('body').css('background',color);
}
});
});
This question already has answers here:
'innerText' works in IE, but not in Firefox
(15 answers)
Closed 7 years ago.
Here is my code
It's working perfect in all browsers but not in Firefox.
I tried many thing but didn't work at all.
Please can some one help me on this issue.
Am I doing something wrong.?
Is there any other way.?
I'M USING .innerText because values are coming from
<span class="jr-rating-wrapper-jr_stars-new-0">
4.5
</span>
There is no error on console.
<script type="text/javascript">
jQuery('#submitButton').click(function(){
var PostStartone = document.getElementById('jr-rating-wrapper-jr_stars-new-0').innerText;
var PostStarSec = document.getElementById('jr-rating-wrapper-jr_stars-new-1').innerText;
var PostStarThird = document.getElementById('jr-rating-wrapper-jr_stars-new-2').innerText;
var PostCapVal = document.getElementById('code').value;
var PostRBVal = "";
var selected = jQuery("div.jr_fieldDiv input[type='radio']:checked");
PostRBVal = selected.val();
jQuery.post("http://xyz/x/Update.php", {
GetStarOneValue : PostStartone ,
GetStarSecValue : PostStarSec ,
GetStarThirdValue : PostStarThird ,
GetCaptchValue : PostCapVal,
GetRadioBTNValue : PostRBVal});
});
</script>
innerText is the "old Internet Explorer" way of doing it.
Try textContent instead. Ideally you should use elem.textContent || elem.innerText, but if you're using jQuery you can just do jQuery("#the_id_here").text().
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
So I have a jquery "checkbox checked/unchecked" function working well. This is a checkbox for turning on or off a particular URL parameter - BUT I believe this code could be written a lot tighter. Does anyone have any suggestions?
$('#mapControl').live('click', function(){
var thisUrl = $(location).attr('href');
if($(this).is(':checked')) {
var lastFour = thisUrl.substr(thisUrl.length - 4);
var param;
if (lastFour == 'com/') {param='?mapControl=true'} else {param='&mapControl=true'}
thisUrl=thisUrl+param;
} else {
$('#urlParam').val(thisUrl);
if (thisUrl.indexOf('?mapControl=true') >= 0){
thisUrl=thisUrl.replace('?mapControl=true','');
} else if (thisUrl.indexOf('&mapControl=true') >= 0){
thisUrl=thisUrl.replace('&mapControl=true','');
}
}
$('#urlParam').val(thisUrl);
});
Try to avoid jQuery as much as you can for example
$('#mapControl').live('click', function(){
// you can directly read window location href attribute
var thisUrl = window.location.href;
var urlParamObj = $('#urlParam');
// instead of $(this).is(':checked') YOU can write *this.checked === true*
if(this.checked === true) {
var lastFour = thisUrl.substr(thisUrl.length - 4);
var param;
if (lastFour == 'com/') {param='?mapControl=true'} else {param='&mapControl=true'}
thisUrl=thisUrl+param;
} else {
urlParamObj.val(thisUrl);
/* if you are sure that your location may have "?mapControl=true" OR "&mapControl=true"you don't have to write code to check string directly replace
*/
thisUrl=thisUrl.replace('?mapControl=true','');
thisUrl=thisUrl.replace('&mapControl=true','');
}
// you don't have to write $('#urlParam') 2 times create a object and refer it again and again
urlParamObj.val(thisUrl);
});