Welcome to WebmasterWorld Guest from 54.158.183.188

Forum Moderators: open

Message Too Old, No Replies

Turning frequently used code into a function

One part of the code changes

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

Full Member

5+ Year Member Top Contributors Of The Month

joined:Sept 30, 2009
posts:227
votes: 1


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];}
4:43 am on Oct 9, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13537
votes: 403


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.
1:16 pm on Oct 9, 2012 (gmt 0)

Full Member

5+ Year Member Top Contributors Of The Month

joined:Sept 30, 2009
posts:227
votes: 1


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];}
7:14 pm on Oct 9, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 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.
7:40 pm on Oct 9, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13537
votes: 403


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 ;)
3:28 am on Oct 10, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 3, 2005
posts:1585
votes: 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.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members