PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Oct 22, 2017 3:56 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Mon Sep 16, 2013 8:39 am 
Offline
Forum Newbie

Joined: Thu Sep 12, 2013 1:39 pm
Posts: 9
Hello;
I would like someone to help me how to grab all accurences of window.addEvent function inside a file.js file, to simplify suppose that the content of that file is inside a variable $string. Here is the content of file.js and i have commented what i want to grab which is :

window.addEvent('domready', function() {
SqueezeBox.initialize({});
SqueezeBox.assign($$('a.modal'), {
parse: 'rel'
});
});


Syntax: [ Download ] [ Hide ]
jQuery(document).ready(function($){
    var $thecontrolBox = $("#o-control-box");
    $("#o-control-box").css({left: parseInt(0 - $thecontrolBox.outerWidth())});
    $("#o-control-handle").click(function() {
                $thecontrolBox.animate({
                left: parseInt($thecontrolBox.css("left"), 10) == 0 ? -$thecontrolBox.outerWidth() : 0
        }, 1000, function(){});
    });
});
               
jQuery(document).ready(function() {
        jQuery('.hasTooltip').tooltip({});
});

jQuery(document).ready(function($) {
        $("#otmenu-desk-wrapper94 ul.ot-dropdown").otmenu({
                direction: 'ltr',
                animation: {opacity:"show",height:"show"},
                        speed: 'normal'
                });
});

// I want to grab this code from here
window.addEvent('domready', function() {
        SqueezeBox.initialize({});
        SqueezeBox.assign($$('a.modal'), {
                parse: 'rel'
        });
});
// to here
       
jQuery(document).ready(function($) {
        $("ul.ot-sliding94").otslmenu({
                speed: 'normal',
                openedHandlerClass: 'icon-minus-sign',
                closedHandlerClass: 'icon-plus-sign'
        });
});

        (function(d, s, id) {
                  var js, fjs = d.getElementsByTagName(s)[0];
                  if (d.getElementById(id)) return;
                  js = d.createElement(s); js.id = id;
                  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
                  fjs.parentNode.insertBefore(js, fjs);
                }(document, 'script', 'facebook-jssdk'));
          (function() {
                 window.___gcfg = {lang: 'en'}; // Define button default language here
                 var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
                 po.src = 'https://apis.google.com/js/plusone.js';
                var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
 


Thank you


Top
 Profile  
 
PostPosted: Mon Sep 16, 2013 12:55 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6573
Location: WA, USA
Can you relax your requirements to "everything from a line that starts with window.addEvent to the line that is exactly an unindented });"?


Top
 Profile  
 
PostPosted: Mon Sep 16, 2013 2:58 pm 
Offline
Forum Newbie

Joined: Thu Sep 12, 2013 1:39 pm
Posts: 9
Hi requinix;

It seem to be difficult like that, i have no garantee that the indentation will remain the seem, because in my example i made it cool and well organized for you but in reality it looks like that :
Syntax: [ Download ] [ Hide ]


                window.addEvent('domready', function() {

                        SqueezeBox.initialize({});
                        SqueezeBox.assign($$('a.modal'), {
                                parse: 'rel'
                        });
                });

                        jQuery(document).ready(function($){
                                var $thecontrolBox = $("#o-control-box");
                                $("#o-control-box").css({left: parseInt(0 - $thecontrolBox.outerWidth())});
                                $("#o-control-handle").click(function() {
                                        $thecontrolBox.animate({
                                                left: parseInt($thecontrolBox.css("left"), 10) == 0 ? -$thecontrolBox.outerWidth() : 0
                                        }, 1000, function(){});
                                });

                        });
               
jQuery(document).ready(function()
                                {
                                        jQuery('.hasTooltip').tooltip({});
                                });
 
                       
       
        jQuery(document).ready(function($) {
                $("#otmenu-desk-wrapper94 ul.ot-dropdown").otmenu({
                        direction: 'ltr',
                        // animation: opacity:"show", height:"show" or combined of them
                        animation: {opacity:"show",height:"show"},
                        // speed: 200 or 'fast', 400 or 'normal', 600 or 'slow'
                        speed: 'normal'
                });
               
        });


       
        jQuery(document).ready(function($) {
                $("ul.ot-sliding94").otslmenu({
                        speed: 'normal',
                        openedHandlerClass: 'icon-minus-sign',
                        closedHandlerClass: 'icon-plus-sign'
                });
               
        });
       

                                (function(d, s, id) {
                                  var js, fjs = d.getElementsByTagName(s)[0];
                                  if (d.getElementById(id)) return;
                                  js = d.createElement(s); js.id = id;
                                  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
                                  fjs.parentNode.insertBefore(js, fjs);
                                }(document, 'script', 'facebook-jssdk'));
                       
                          (function() {
                                window.___gcfg = {lang: 'en'}; // Define button default language here
                            var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
                            po.src = 'https://apis.google.com/js/plusone.js';
                            var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
                          })();
                       
 

So what i suggest to simplify it, is that there is always jQuery(document).ready after the latest });. So i suggest to grab anything between window.addEvent and jQuery(document).ready, than i will remove white spaces my self, this is my own workarround but if there is something even better than ok.
Thank you


Top
 Profile  
 
PostPosted: Mon Sep 16, 2013 3:32 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6573
Location: WA, USA
So long as the event handler won't try to use "jQuery(document).ready" in its code,
Code:
window\.addEvent\(.*?(?=jQuery\(document\)\.ready)

The problem with your initial requirements is that the regex would need to be able to "parse" Javascript so that it could understand where the function ended. It's not impossible, but it's a recursive hassle that would be really nice to avoid.


Top
 Profile  
 
PostPosted: Mon Sep 16, 2013 6:53 pm 
Offline
Forum Newbie

Joined: Thu Sep 12, 2013 1:39 pm
Posts: 9
Thank you, i tried that code :
Syntax: [ Download ] [ Hide ]
echo $js; // Output above code correcty
$patern = '/window\.addEvent\(.*?(?=jQuery\(document\)\.ready)/';
preg_match($patern, $js, $out);
print_r($out);
 

It output void array, why ?
Syntax: [ Download ] [ Hide ]
Array ( )

So an alternative and i have found a good alternative, instead of searching jquery(document) there is something easier and cleaner also :D
Syntax: [ Download ] [ Hide ]
                window.addEvent('domready', function() {

                        SqueezeBox.initialize({});
                        SqueezeBox.assign($$('a.modal'), {
                                parse: 'rel'
                        }); // <- No empty line after it, so there is no \n\n
                }); // <- Yep, there is empty line after it (\n\n)

 


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group