homepage Welcome to WebmasterWorld Guest from 54.205.189.156
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Turning frequently used code into a function
One part of the code changes
ocon




msg:4505717
 2:44 am on Oct 9, 2012 (gmt 0)

I have several lines of code that I reuse in my script that I would like to turn it into a function. Unfortunately one part of the code that changes. Can I still turn it into a function?

Sometimes it's used like this:
if(p.ll||p.m||p.a){
var url = window.location.href.split("?");
var pr = url[1].split(/[&;]/g);
for(var i=0;i<pr.length;i++) if((pr[i].lastIndexOf("ll=", 0)!==-1) || (pr[i].lastIndexOf("m=", 0)!==-1) || (pr[i].lastIndexOf("a=", 0)!==-1)) pr.splice(i, 1);
window.location = (pr.length>0) ? url[0]+"?"+pr.join("&") : url[0];}


Other times it's used like this:
if(p.x){
var url = window.location.href.split("?");
var pr = url[1].split(/[&;]/g);
for(var i=0;i<pr.length;i++) if(pr[i].lastIndexOf("x=", 0)!==-1) pr.splice(i, 1);
window.location = (pr.length>0) ? url[0]+"?"+pr.join("&") : url[0];}

 

lucy24




msg:4505754
 4:43 am on Oct 9, 2012 (gmt 0)

Are there just two forms?

if ((pr[i].lastIndexOf("ll=", 0)!==-1) || (pr[i].lastIndexOf("m=", 0)!==-1) || (pr[i].lastIndexOf("a=", 0)!==-1))

and

if (pr[i].lastIndexOf("x=", 0)!==-1)

Or did you mean that the generic pattern is

if (p.one||p.two||p.three||et cetera at random)
...
if ((pr[i].lastIndexOf("one=", 0)!==-1) || (pr[i].lastIndexOf("two=", 0)!==-1) || (pr[i].lastIndexOf("three=", 0)!==-1) || et cetera for any number of p.thingies)


?
Makes a big difference to the question.

ocon




msg:4505908
 1:16 pm on Oct 9, 2012 (gmt 0)

Right now there are just two forms, and that's all I can see in the near future.

I didn't think about that before, but should I just pass some kind of variable to the function to tell it which form I want to use it in and then use a switch inside the function for which part of the code to execute?

if(type){
var url = window.location.href.split("?");
var pr = url[1].split(/[&;]/g);
if(type==1) for(var i=0;i<pr.length;i++) if(pr[i].lastIndexOf("x=", 0)!==-1) pr.splice(i, 1);
else for(var i=0;i<pr.length;i++) if((pr[i].lastIndexOf("ll=", 0)!==-1) || (pr[i].lastIndexOf("m=", 0)!==-1) || (pr[i].lastIndexOf("a=", 0)!==-1)) pr.splice(i, 1);
window.location = (pr.length>0) ? url[0]+"?"+pr.join("&") : url[0];}

g1smd




msg:4506056
 7:14 pm on Oct 9, 2012 (gmt 0)

You could.

There's several right ways of writing the code, and many wrong ways.

As projects develop, you'll likely refactor your code at least several times.

lucy24




msg:4506083
 7:40 pm on Oct 9, 2012 (gmt 0)

Personally I like subfunctions, but that's coding style.

flag = checkIt(any necessary variables here)
if (flag == 1)
{ do your stuff}

where fn checkIt does something like

newflag = 0;
if (lastIndexOf blahblah etc) {newflag = 1}
if (lastIndexOf other blahblah etc) {newflag = 1}
etc
return newflag

With exact wording based on language.

Generally anything that happens the same way two or more times goes in a subroutine of some kind, unless it involves tossing around variables that are so huge it would slow down the whole thing. Well, that's a www universal isn't it? It's why CSS and Includes were invented ;)

daveVk




msg:4506286
 3:28 am on Oct 10, 2012 (gmt 0)

To check all entries in array, pr.splice(i, 1) needs to be pr.splice(i--, 1) ?.

If you remove say pr[3], what was pr[4] becomes pr[3] and index should not be incremented.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved