javascript - includes() not working in all browsers -


right here block of code. works perfect in firefox , chrome. not in ie. error "object doesn't support property or method 'includes'"

function righttreeswapfunc2() {     if ($(".right-tree").css("background-image").includes("stage1") == true) {         $(".right-tree").css({             backgroundimage: "url(/plant-breeding/img/scenes/plant-breeding/stage5.jpg)"         })     } else {         $(".right-tree").css({             backgroundimage: "url(/plant-breeding/img/scenes/plant-breeding/stage3.jpg)"         })     } } 

i change bit , use vanilla js , do:

document.getelementbyid("right-tree").classlist.contains

but rather see if there way work in ie before changing js , editing html , css.

if @ documentation of includes(), of browsers don't support property.

you can use supported indexof() after converting property string using tostring():

if ($(".right-tree").css("background-image").indexof("stage1") > -1) { //                                           ^^^^^^^^^^^^^^^^^^^^^^ 

you can use polyfill mdn.

if (!string.prototype.includes) {     string.prototype.includes = function() {         'use strict';         return string.prototype.indexof.apply(this, arguments) !== -1;     }; } 

Comments