Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript .Replace Alternative

Tags:

javascript

css

I am coding a template for eBay. However, eBay does not allow .replace. The code below is for a rollover tab section.When the user hovers over tab(a), the correspodning div div(a) is made to become visible.

Is there a workaround to get the code to work without using .replace?

var divs = new Array();
divs.push("contentPayment");
divs.push("contentShipping");
divs.push("contentWarranty");
divs.push("contentContact");
var navs = new Array();
navs.push("nav1");
navs.push("nav2");
navs.push("nav3");
navs.push("nav4");
///////////////////////////////////////


function hasClass(element, cls) {
    return (' ' + element.className + ' ').indexOf(' ' + cls + ' ') > -1;
}
///////////////////////////////////////////////////////////////////////


function toggleDisplay(id) {
    for (var i = 0; i < divs.length; i++) {
        var item = document.getElementById(divs[i]);
        item.style.display = 'none';
    }
    var target = document.getElementById(id);
    target.style.display = 'block';
    /////////////////////////////////////////////////////////////////////////////////////////////////////// 
    ////////////////////////////PAYMENT IS HOVERED////////////////////////////////////////////////////////
    if (id == "contentPayment") {
        var CurrentTab = document.getElementById("nav1");
        var AlreadyActive = hasClass(CurrentTab, "tabActive");
        if (AlreadyActive === false) {
            for (var i = 0; i < navs.length; i++) {
                var otherTabs = document.getElementById(navs[i]);
                otherTabs.className = otherTabs.className.replace(' tabActive', '');
            }
            CurrentTab.className += " " + "tabActive";
        }
    }

    /////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////Shipping IS HOVERED////////////////////////////////////////////////////////
    if (id == "contentShipping") {
        var CurrentTab = document.getElementById("nav2");
        var AlreadyActive = hasClass(CurrentTab, "tabActive");
        if (AlreadyActive === false) {
            for (var i = 0; i < navs.length; i++) {
                var otherTabs = document.getElementById(navs[i]);
                otherTabs.className = otherTabs.className.replace(' tabActive', '');
            }
            CurrentTab.className += " " + "tabActive";
        }
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////Warranty IS HOVERED////////////////////////////////////////////////////////
    if (id == "contentWarranty") {
        var CurrentTab = document.getElementById("nav3");
        var AlreadyActive = hasClass(CurrentTab, "tabActive");
        if (AlreadyActive === false) {
            for (var i = 0; i < navs.length; i++) {
                var otherTabs = document.getElementById(navs[i]);
                otherTabs.className = otherTabs.className.replace(' tabActive', '');
            }
            CurrentTab.className += " " + "tabActive";
        }
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////Contact IS HOVERED////////////////////////////////////////////////////////
    if (id == "contentContact") {
        var CurrentTab = document.getElementById("nav4");
        var AlreadyActive = hasClass(CurrentTab, "tabActive");
        if (AlreadyActive === false) {
            for (var i = 0; i < navs.length; i++) {
                var otherTabs = document.getElementById(navs[i]);
                otherTabs.className = otherTabs.className.replace(' tabActive', '');
            }
            CurrentTab.className += " " + "tabActive";
        }
    }
}
like image 421
user1698144 Avatar asked Oct 07 '12 15:10

user1698144


People also ask

Does JavaScript have replaceAll?

The replaceAll() method returns a new string with all matches of a pattern replaced by a replacement . The pattern can be a string or a RegExp , and the replacement can be a string or a function to be called for each match.

How do you replace a sentence in JavaScript?

To replace text in a JavaScript string the replace() function is used. The replace() function takes two arguments, the substring to be replaced and the new string that will take its place. Regex(p) can also be used to replace text in a string.

What is replace (/ g in JavaScript?

The "g" represents the "global modifier". This means that your replace will replace all copies of the matched string with the replacement string you provide.

What is the difference between Replace and replaceAll in JavaScript?

3.1 The difference between replaceAll() and replace() If search argument is a string, replaceAll() replaces all occurrences of search with replaceWith , while replace() only the first occurence. If search argument is a non-global regular expression, then replaceAll() throws a TypeError exception.


2 Answers

You may try this as an alternative of replace function

String.prototype.fakeReplace = function(str, newstr) {
    return this.split(str).join(newstr);
};

var str = "Welcome javascript";
str = str.fakeReplace('javascript', '');
alert(str); // Welcome

DEMO.

like image 100
The Alpha Avatar answered Oct 22 '22 23:10

The Alpha


For a more efficient, but slightly longer method, use this:

String.prototype.myReplace = function(pattern, nw) {
   var curidx = 0, len = this.length, patlen = pattern.length, res = "";
   while(curidx < len) {
       var nwidx = this.indexOf(pattern, curidx);
       console.log(nwidx);
       if(nwidx == -1) {
           break;
       }
       res = res + this.substr(curidx, nwidx - curidx);
       res = res + nw;
       curidx = nwidx + patlen;
   }
   return res;
};
alert("Glee is awesome".myReplace("awesome", "very very very awesome"));

See it in action: little link.

Hope that helped!

like image 40
Chris Avatar answered Oct 22 '22 23:10

Chris