How do I replace eval in the following script?

Discussion in 'jQuery' started by Laurence Lewis, Nov 23, 2016.

  1. #2
    Hi
    JSLint and Dreamweaver report eval as dangerous. How do I:
    1. replace 'eval' in the script
    2. move the function outside the loop?

    I am learning so any help is appreciated.

    /* exported popup_default , popup_help , popup_sitemap , popup_footerlinks */
    
    var matchClass=['popup_default' , 'popup_sitemap','popup_footerlinks', 'popup_help'];
    var newwindow = ''
      
        var popup_default = 'width=800,height=640,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=250,top=200';  
        var popup_help = 'width=700,height=650,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=100,top=100';
        var popup_sitemap = 'width=1000,height=600,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=100,top=100';  
        var popup_footerlinks = 'width=800,height=500,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=250,top=200';
      
        function pop_ups(){
            "use strict";
            var x = 0;
            var popClass;
            while(x < matchClass.length){
                    popClass = "'."+matchClass[x]+"'";
                    $(eval(popClass)).click(function() {
                            var popurl = $(this).attr('href');
                            var popupSpecs = $(this).attr('class');
                            var popupName = Math.floor(Math.random()*10000001);
                            newwindow=window.open(popurl,popupName,eval(popupSpecs));
                            return false;
                    });                          
            x++;
            }
        }
    
        $(function() {
        "use strict";
        pop_ups();
        });
    Code (markup):
     
    Laurence Lewis, Nov 23, 2016 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,807
    Likes Received:
    4,534
    Best Answers:
    123
    Trophy Points:
    665
    #3
    The first eval doesn't look like it's doing anything, and I'd want to see a console.log() of
    eval(popupSpecs)
    and
    popupSpecs
    because I don't think that one is doing anything either.
     
    sarahk, Nov 23, 2016 IP
  3. Laurence Lewis

    Laurence Lewis Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    Thanks @sarahk It was confusing me. I have it re-writted like this and it seems to work:

    /* exported popup_default , popup_help , popup_sitemap , popup_footerlinks */
    
    function makePopup(className, specs) {
        "use strict";
        $('.'+className).click(function(e) {
            e.preventDefault();
            var popupName = Math.floor(Math.random()*10000001);
            window.open(this.href, popupName, specs);
        });
    }
    var popups = {
        popup_default:     'width=800,height=640,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=250,top=200',
        popup_help:        'width=700,height=650,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=100,top=100',
        popup_sitemap:    'width=1000,height=600,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=100,top=100',
        popup_footerlinks: 'width=800,height=500,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=250,top=200'
    };
    
    $(function() {
        "use strict";
        for (var key in popups) {
            if(popups.hasOwnProperty(key))
            {
              makePopup(key, popups[key]);
            }
        }
    });
    Code (markup):
     
    Last edited by a moderator: Nov 24, 2016
    Laurence Lewis, Nov 23, 2016 IP