Concatenated Domain Name in JavaScript [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've taken over the development of a project and have noticed a weird snippet in the JavaScript where the developer wrote the following:
/* ... code */
var el = document.getElementById('foo');
el.href = "http://" + "w" + "w" + "w" + "." + "d" + "o" + "main.com/foobar/";
/* ... code */
I have some hunches as to what the purpose is, but will refrain from expressing it so as to not misguide, probably better, answers ...
What is the purpose of concatenating the domain?

In terms of JavaScript itself, this has practically no effect - the result is the same.
But the reason may be different than to accomplish some task in JavaScript. I guess there are two possibilities that are most likely to be the case here:
To mislead other programmers (so the domain name is not easily found by simple text search). Similar (but a lot more complex) ways are used by worms to insert code into the website without showing what it contains, unless you will put a lot more effort to analyse it.
To try to mislead crawlers, which probably assumes they are not parsing the JavaScript and getting actual result. It may be the case for example if the programmer feared that the code will be eg. indexed and by searching this domain name in the search engine, anyone can find out it was mentioned in the code of the site you are describing.

Well normally you would be concatenating this because you would use variables within it...
el.href = "http://www." + domain + "." + ext "/" + additionalUrl;
Otherwise separating letter by letter like that serves no purpose. I'm assuming the previous programmer was just bored :)

Related

Data transform in Node.js, best practices? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I have a need of transforming various message formats into JSON and v.v., e.g. XML to JSON and then JSON to XML or EDI to JSON and JSON to EDI.
I've looked at several different XML-to-JSON modules and they seem mostly to be into a direct conversion into their own JSON and/or XML format and not into my required XML (e.g. UBL 2.1).
One easy straight forward way of going about it is to just use a String variable:
let myXML = '<root><hdr>' + jsonIn.hdr + '</hdr>\r\n';
myXML += '<itm>' + jsonIn.item[0] + '</hdr></root>';
The myXML variable will be quite big though. Up to 200 kB currently but can grow bigger in the future.
Obviously this is the quickest and easiest way of creating the outbound formats but it doesn't really feel right to create a massive String variable...
In Java I'd use StringBuilder and there is a npm for Node: https://www.npmjs.com/package/stringbuilder
Which approach would you consider the "best practice" approach?
In Java I'd use StringBuilder...
If that's the case and so you do in the end need to end up with a single string containing the result, a fairly normal pattern is to build up the individual strings in an array, and then use Array#join when you're done to produce that one big final string:
let myXML = [];
myXML.push('<root><hdr>' + jsonIn.hdr + '</hdr>\r\n');
myXML.push('<itm>' + jsonIn.item[0] + '</hdr></root>');
// ...
// When you're ready for the big string:
myXML = myXML.join("");
If you don't need a big string at the end, but are writing to a file, etc., writing as you go would tend to be a good solution.

Return join vs return string [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I saw this example and wondered why anyone would do it:
function a(str) {
return [
'<div>',
str,
'</div>'
].join('');
}
Isn't it an equivalent to the following code and what's the advantages / disadvantages of using just:
function a(str) {
return '<div>' +
str +
'</div>;
}
Thank you.
They're the same thing. Some experiments have shown using the + operator is faster but this will vary between browsers. Not to mention, these kinds of micro-optimizations don't tend to contribute much.
So which one is better? Whichever one you like the most.
My preference would be to use the "string concatenation" version, in other words, your second one.
They both return the same thing. In the case of the first example however your combining string elements of an array together. In the second one, you're simply adding the strings together.
There's no need to use an array to solve this problem, and the second one is much more concise too. It's a better web development practice.
Performance wise, there will be a slight improvement with the second one. Since it has less space, that means the JavaScript file that loads it will be smaller, taking up fewer resources on a user's browser.
It will also make it more easily maintainable with fewer lines of code.

Generic functions: JavaScript or PHP? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
While working on a recent project, I began wondering when somebody may use JavaScript vs. PHP for a generic function. Take this basic function (in JS) as an example, which simply returns whether or not a number falls within a particular range:
function range(num, var1, var2) {
if ((num >= var1) && (num <= var2)) {
return true;
}
else {
return false;
}
}
For something that doesn't query a database, nor is it information that should be — or needs to be — indexed for SEO (I know by default JavaScript will not be indexed), then my inference would be that JavaScript would be sufficient. But at the same time, PHP could be as well.
Basically, if the ONLY point of the application were a simple function like above (not that I can see a reason for that, but I digress...), then which langauge would be better to write this in? JavaScript or PHP?
Would love any insight as to which would be the best method to use and why. I recognize there is no right or wrong answer necessarily, but would like to hear arguments for or against one over the other.
Thanks!
As you point out, there is no necessarily right or wrong answer.
I would say that it depends:
-Is it a problem for people to be able to reverse engineer the code?
-Is it a problem if it does not execute because JavaScript might be disabled?
-Is it preferable to have code execute client-side versus server-side from a performance point of view?
-Does the content generated by the output of this function qualify as something you might want indexed by Search Engines?
Depending on the importance of the above criteria/questions, JavaScript might be disqualified.
From the points above, the common thing seems to be that choosing for JavaScript is more likely to lead to potentially undesirable side-effects.
The safest bet, from what I theorize, is therefore the server-side language, PHP.

Javascript http regex [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am currently using the following to convert [url=][/url] to a html link:
s = message.replace(/\[url=([^\]]+)\]\s*(.*?)\s*\[\/url\]/gi, "<a href='$1'>$2</a>")
That work's fine.
I then added on another replace function using a regex to replace www with http://www like so:
s = message.replace(/\[url=([^\]]+)\]\s*(.*?)\s*\[\/url\]/gi, "<a href='$1'>$2</a>")
.replace(/www/g, "http://www");
This is probably not the best/efficient method and also does not support https:// which is not a priority at the moment but is something I would like to include at some point. Could someone please advise me on what I could do to improve the regex?
Couple of things:
First, there are some problems with your second pattern. It is not case insensitive (whereas the first pattern is) so it won't catch things like 'WWW'. Maybe that's desirable, maybe not. But it's also global, and is not anchored to the beginning of the URL. So it will replace www anywhere in the URL. Changing it to something like /href=\'www/ and then changing the replace string to href='http://www should solve these problems.
Secondly, in a case like this, using 2 regular expressions may not be bad. You can fold it into one regex if you want, but that doesn't mean it is any more efficient for the computer or the poor human who happens to be reading it.
All that said, one way to accomplish this with a single regular expression, is to do something like:
s = message.replace(/\[url=(?:http:\/\/)?([^\]]+)\]\s*(.*?)\s*\[\/url\]/gi, "<a href='http://$1'>$2</a>")
This may accomplish what you want. It does not key off of "www", though, it simply prepends "http://" to any URL that does not already begin with that string. It also doesn't support https, as you mentioned, but I'm not sure how you will support that anyway. If all you are given is a URL with no protocol, how do you determine whether or not to make it https?

How to check which part of page is an article? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I would like to create a similar tool to Instapaper or Readability and I wonder what is the best way to find and get text from a web page. Do you have any ideas?
The question is too broad to give a concrete answer to, but you can separate this question into three concerns:
A way to grab web resources. libcurl for example, or just about anything able to talk HTTP.
A DOM parser. Python has xml.dom.minidom, for example.
An algorithm for traversing the DOM tree and extracting text. Be it scanning for elements with class=article, or <div>s with more than 1024 characters etc., is entirely up to you. You will need experimentation to get this right.
I suggest asking separate questions for each of these concerns. After doing research on each, of course. :)
Here is an idea to get you started in Ruby. Just tested the code below and it is working fine for me. Have a look it might help you.
require 'open-uri'
require 'cgi'
require 'nokogiri'
$url='http://www.stackoverflow.com'
$txt_file = open($url)
$raw_contents = $txt_file.read
$html = Nokogiri::HTML(CGI.unescapeHTML($raw_contents)).content
#strip the web page fetched out of all hmtl tags and encoded chars
$txt_file = File.new('c:\ruby193\bin\web-content\stack.txt', "w")
#stack.txt now contains a stripped, pure txt file which you can manipulate further
$txt_file.write($html)
$txt_file.close
puts 'Here is the stripped text of your webpage\n'+$html

Categories

Resources