Jump to content

All this line of code.. javascript?


Monkuar

Recommended Posts

function uc(o,f){var h="";for(var i=0;i<o.childNodes.length;i++){var x=o.childNodes[i],a="";switch(x.tagName?x.tagName:x.nodeName){case "#text":h+=x.nodeValue;break;case "BR":h+="\n";break;case "EMBED":if(x.src.indexOf("mp3.swf")!=-1)break;case "B":h+="[b]";a="[/b]";break;case "U":h+="[u]";a="[/u]";break;case "I":h+="[i]";a="[/i]";break;case "S":h+="[s]";a="[/s]";break;case "CENTER":h+="
[center]";a="[/center]
";break;case "A":var v=x.href,c="URL";if(v.indexOf("javascript")!=-1){v=v.replace(/.+?['"](.+?)['"].+?/,"$1")}else{if(v.indexOf("mailto:")!=-1){v=v.substr(7,v.length);c="EMAIL"}}h+="["+c+"="+v+"]";a="[/"+c+"]";break;case "IMG":if(x.alt){h+=x.alt;}else{h+="[img="+x.src+"]";}break;case "SPAN":if(x.className=="edit"){continue}if(x.style.color){h+="[color="+x.style.color+"]";a="[/color]"}else{if(x.style.fontFamily){h+="[font="+x.style.fontFamily+"]";a="[/font]"}else{if(x.style.fontSize){h+="[size="+(parseInt(x.style.fontSize)-7)+"]";a="[/size]"}}}break;case "DIV":if(x.style.textAlign=="right"){h+="
[right]";a="[/right]
"}else{if(x.className=="quote1"){if(x.childNodes[0]&&x.childNodes[0].nodeName=="#text"){var v=x.childNodes[0].nodeValue;if(v.indexOf("uote")!=-1){if(f&1){continue}h+="[quote";if(v&&v.length>6){h+=v.replace(/Quote \((.+?) @ (.+?)\)/i,"=$1,$2")}h+="]";continue}else{h+="[code]";continue}}}else{if(x.className=="quote2"){if(f&1){continue}a="[/quote]"}else{if(x.className=="code2"){a="

"}}}}break;case "OL":case "UL":h+="[list"+(x.type?"=+x.type:"")+]";a="

";break;case "LI":h+="[*]";a="\n";break}h+=uc(x,f)+a}return h.replace(/^\s+/,"").replace(/\s+$/,"")}

[/code]

 

is there a program that breaks it down so it's not 100%width horizontal and it does a break tag after each ; so it's easier to read? is there a program that would do it?

 

thx

Link to comment
Share on other sites

What do you mean it breaks a tag after each ;?

 

If you want it easier to read, then format it yourself. Generally something compressed like that works so you don't have to break it down unless you want to edit it. Also, the browser parses a compressed script faster.

Link to comment
Share on other sites

What do you mean it breaks a tag after each ;?

 

If you want it easier to read, then format it yourself. Generally something compressed like that works so you don't have to break it down unless you want to edit it. Also, the browser parses a compressed script faster.

I was kinda wondering something about this recently. Doesn't that kind of prevent people from using your source as well? I mean if you have like 500+ lines of JavaScript and you remove all the unnecessary whitespace and make it practically unreadable who is really going to uncompress it?

Link to comment
Share on other sites

I was kinda wondering something about this recently. Doesn't that kind of prevent people from using your source as well? I mean if you have like 500+ lines of JavaScript and you remove all the unnecessary whitespace and make it practically unreadable who is really going to uncompress it?

Well, for the most part, JavaScript is dependant on the site, so it's not like you can just copy some JavaScript, slap it on your site and it'll work. For those people who don't know how to code, it wouldn't matter. I could apply the proper indentation and those people will still not understand it. But for those who know JavaScript, they would just write their own.

Link to comment
Share on other sites

I was kinda wondering something about this recently. Doesn't that kind of prevent people from using your source as well? I mean if you have like 500+ lines of JavaScript and you remove all the unnecessary whitespace and make it practically unreadable who is really going to uncompress it?

Well, for the most part, JavaScript is dependant on the site, so it's not like you can just copy some JavaScript, slap it on your site and it'll work. For those people who don't know how to code, it wouldn't matter. I could apply the proper indentation and those people will still not understand it. But for those who know JavaScript, they would just write their own.

The only real benefit of removing the white space is to make it run faster? Is the speed increase even worth it?

Link to comment
Share on other sites

I was kinda wondering something about this recently. Doesn't that kind of prevent people from using your source as well? I mean if you have like 500+ lines of JavaScript and you remove all the unnecessary whitespace and make it practically unreadable who is really going to uncompress it?

Well, for the most part, JavaScript is dependant on the site, so it's not like you can just copy some JavaScript, slap it on your site and it'll work. For those people who don't know how to code, it wouldn't matter. I could apply the proper indentation and those people will still not understand it. But for those who know JavaScript, they would just write their own.

I disagree. If Java Script (or any code) is properly written it should be able to be used on other sites/applications with little to no modification. If that is not the case, the person writing the script is doing themself a disservice in writing non-modularized code.

 

But, to answer your questions, people may compact the code to run faster or as a simple means of obfruscating the code (i.e. making it hard for others to read) to protect it. But, just as theri are tools to do this for you there are others to reverse it. There are some that even go as far as transcoding characters into equivalent character codes making it even more difficult (but not impossible) to reverse engineer.

 

The bottom line is that you cannot "protect" your javascript code. You can take steps to make it harder for someone else to use, but that also introduces problems in maintaining the code as well. I think obfuscation was the intent of the code posted above.

Link to comment
Share on other sites

mjdamato - I said for the most part. Like if I were to add some JavaScript to my site for a form validation, I would write it as is rather than abstracting it 100 folds and complicating the process. I mean it will be abstracted enough for me to re-use on another form, but not for *any* form. It's not like I'm writing a JavaScript library in the process.

 

Edit: A better example would be a lightbox modal pop up.

Link to comment
Share on other sites

I still disagree. IMHO, a form validation should be completely modularized into the different validations such that they can be easily reused. The main function that is called upon submission would simply call the various validation functions (e.g. isEmail(), isEmpty(), etc.). It would be fairly simple for someone to pick up and reuse on another form with little modification - just call the appropriate funstion(s) for each field. That's the whole point - I don't want to rewrite code if I don't need to.

Link to comment
Share on other sites

Yes but I would have some JavaScript like -

function validate () {
     if (/some_regex/.test(document.getElementById('someid').value)) {
          // do something
     }
}

 

I guess one can pass parameters to the validate function, but to keep my HTML clean, I don't. Now assuming someone else uses the same IDs as I have, it may work. The regex may need to be edited as well. Otherwise, they'll be editing the code to suit their needs. And for the most part, they'll end up fixing up the entire function because their form fields are different than mine. Which goes back to my point that if a coder can write it, they'll just write it. I think it's easier to start from scratch rather than getting someone else's code and modify almost the same number of lines. But first, he/she would need to read script just to understand what it does and how it works.

Link to comment
Share on other sites

I tend to work the other way round now, due to libraries like jquery.

 

I'll write a normal html code as I like it then use a jquery selector to get the relevant form inputs and test them on blur, onkeypress, onchange, whichever is best at the time. If encessary I'll add some rel="" information to help out the form;

 

<form>
<input type="text" name="field" class="validate" rel="alphanum,max=30,min=10" />
<input type="submit" name="submit" />
</form>

 

My jquery would check the input fiel named 'field' via it's class name. Then it can get the data from the rel attribute, and run functions as required returning true or false for the arguments passed.

 

This would leave me with fairly simple javascript that was never changed. I just need to add a simple class name and a few bits of data to rel in my form.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.