Issue with Javascript boolean values - javascript

I wrote a small script to recreate the Chrome address bar, wherein my code checks an input for any domain extension(.com, .edu etc.) and sets a boolean flag to true if an extension is found.
It then checks the flag and based on the result opens the website or sends it to google as a query.
Additionally, if it is a website, it checks if the string contains http:// and www. and if not, adds it to the string before using Window.Open() to open the target.
What's wrong here?
function openSite(){
var domain_extensions = [".aero", ".asia", "...All Other Extensions...", ".zr", ".zw"];
var isSite = false;
var userIn = document.getElementById('in_field').value; //Retrieves Textbox code
for (var i=0; i < domain_extensions.length; i++)
if (userIn.search(domain_extensions[i]) !==-1)
isSite = true;
//Checks against the array of extensions
if (isSite === true){
if (userIn.search("http://") === -1 || userIn.search("https://") === -1)
{if(userIn.search("www.") === -1)
userIn = "http://www." + userIn;
else
userIn = "http://" + userIn;
}
window.open(userIn, '_blank');
//if extension is found, open website
//if qualifier http:// or https:// and/or www. not found, append and open website
}
else{
var str = encodeURI("http://www.google.com/search?q=" + userIn);
window.open(str, '_blank');
} //Searches query for common extensions; if not found search google
}

This is a problem, I believe, with your usage of the search function. This function takes a regular expression as its argument. The . character is special in regex, and matches any character.
For example:
var test = "blasdfahsadfcomasdfasd";
console.log(test.search(".com")); // prints 11
Prepend the . with a backslash in order to override this behavior:
var test = "blasdfahsadfcomasdfasd";
console.log(test.search("\\.com")); // prints -1
Additionally, if you want to check only at the end of a string, add a $ symbol at the end of the strings like so:
var test = "blasdfahsadf.comasdfasd";
console.log(test.search("\\.com$")); // prints -1; prints 12 w/o the $

Related

Javascript return true if a string contains subdomain

I have an API that returns a domain to my front end.
This domain is the string format.
For eg: "google.com" / "google.co.ok"
or "test.google.com"/ "test.google.co.ok:
Notice that the string does not contain any protocol.
I want to write a method that parses the string and returns true if the string contains a subdomain.
In the above 2 examples, the method should return true for test.google.com or test.google.co.ok
EDIT: If it were python, i would write something like below. But hoping something similat was available in JS.
from tld import get_tld, get_fld
get_tld("www.google.co.uk", fix_protocol=True)
# 'co.uk'
get_fld("www.google.co.uk", fix_protocol=True)
# 'google.co.uk'
There are multiple JavaScript libraries available that can be used the same way you're using tld. psl is older but still has millions of weekly downloads.
You could use psl and implement something like this:
import { parse } from "psl";
function hasSubdomain(str) {
const { subdomain } = parse(str);
return subdomain !== null;
}
hasSubdomain("www.google.com") // true
hasSubdomain("google.co.uk") // false
Feel free to clone and edit this example on RunKit as you see fit.
Sure thing. Since there's no protocol, maybe something like:
"word.domain.com"
.split(".").length > 2 // true
"domain.com"
.split(".").length > 2 // false
"www.domain.co.uk"
.split(".").length > 2 // uh-oh
You'll likely need to parse out "www" and second-level domains (".co", ".gc", etc).
You can use RegExp to perform string manipulation. Please take a look at the following snippet and run the code and see the results from different test cases covering most of the possibilities. Let me know if it's helpful.
function subDomain(url) {
// REMOVE LEADING AND TRAILING WHITE SPACE
url = url.replace(new RegExp(/^\s+/), ""); // START
url = url.replace(new RegExp(/\s+$/), ""); // END
// CONVERT BACK SLASHES TO FORWARD SLASHES
url = url.replace(new RegExp(/\\/g), "/");
// REMOVES 'www.' FROM THE START OF THE STRING
url = url.replace(new RegExp(/^www\./i), "");
// REMOVE STRING FROM FIRST FORWARD SLASH ON
url = url.replace(new RegExp(/\/(.*)/), "");
// REMOVES '.??.??' OR '.???.??' FROM END - e.g. '.CO.UK', '.COM.AU'
if (url.match(new RegExp(/\.[a-z]{2,3}\.[a-z]{2}$/i))) {
url = url.replace(new RegExp(/\.[a-z]{2,3}\.[a-z]{2}$/i), "");
// REMOVES '.??' or '.???' or '.????' FROM END - e.g. '.US', '.COM', '.INFO'
} else if (url.match(new RegExp(/\.[a-z]{2,4}$/i))) {
url = url.replace(new RegExp(/\.[a-z]{2,4}$/i), "");
}
// CHECK TO SEE IF THERE IS A DOT '.' LEFT
var subDomain = url.match(new RegExp(/\./g)) ? true : false;
return subDomain;
}
const subdomainInput = "test.google.com";
const subdomainInputWithPath = "test.google.com/test";
const subdomainInputWithPathWithWS = " test.google.com ";
const subdomainInputWithWS = " test.google.com ";
const subdomainInputWithQueryString = "test.google.com/test?token=33333";
const noSubInput = "google.com"
const noSubInputWithPath = "google.com/search"
const noSubInputWithPathWithQueryString = "google.com/search?token=ttttttt"
console.log("Test Run\n")
conosle.log("With subdomain test cases")
console.log(`subdomainInput: ${subDomain(subdomainInput)}`);
console.log(`subdomainInputWithPath: ${subDomain(subdomainInputWithPath)}`);
console.log(`subdomainInputWithWS: ${subDomain(subdomainInputWithWS)}`);
console.log(`subdomainInputWithPathWithWS: ${subDomain(subdomainInputWithPathWithWS)}`);
console.log(`subdomainInputWithQueryString: ${subDomain(subdomainInputWithQueryString)}`);
conosle.log("Without subdomain test cases")
console.log(`noSubInput: ${subDomain(noSubInput)}`);
console.log(`noSubInput: ${subDomain(noSubInput)}`);
console.log(`noSubInputWithPath: ${subDomain(noSubInputWithPath)}`);
console.log(`noSubInputWithPathWithQueryString: ${subDomain(noSubInputWithPathWithQueryString)}`);
return(subDomain);
}

JavaScript Regex URL extract domain only

Currently I can extract the 'domain' from any URL with the following regex:
/^(?:https?:\/\/)?(?:[^#\n]+#)?(?:www\.)?([^:\/\n\?\=]+)/im
However I'm also getting subdomain's too which I want to avoid. For example if I have sites:
www.google.com
yahoo.com/something
freds.meatmarket.co.uk?someparameter
josh.meatmarket.co.uk/asldf/asdf
I currently get:
google.com
yahoo.com
freds.meatmarket.co.uk
josh.meatmarket.co.uk
Those last two I would like to exclude the freds and josh subdomain portion and extract only the true domain which would just be meatmarket.co.uk.
I did find another SOF that tries to solve in PHP, unfortunately I don't know PHP. is this translatable to JS (I'm actually using Google Script FYI)?
function topDomainFromURL($url) {
$url_parts = parse_url($url);
$domain_parts = explode('.', $url_parts['host']);
if (strlen(end($domain_parts)) == 2 ) {
// ccTLD here, get last three parts
$top_domain_parts = array_slice($domain_parts, -3);
} else {
$top_domain_parts = array_slice($domain_parts, -2);
}
$top_domain = implode('.', $top_domain_parts);
return $top_domain;
}
So, you need firstmost hostname stripped from your result, unless there only two parts already?
Just postprocess your result from first match with regexp matching that condition:
function domain_from_url(url) {
var result
var match
if (match = url.match(/^(?:https?:\/\/)?(?:[^#\n]+#)?(?:www\.)?([^:\/\n\?\=]+)/im)) {
result = match[1]
if (match = result.match(/^[^\.]+\.(.+\..+)$/)) {
result = match[1]
}
}
return result
}
console.log(domain_from_url("www.google.com"))
console.log(domain_from_url("yahoo.com/something"))
console.log(domain_from_url("freds.meatmarket.co.uk?someparameter"))
console.log(domain_from_url("josh.meatmarket.co.uk/asldf/asdf"))
// google.com
// yahoo.com
// meatmarket.co.uk
// meatmarket.co.uk
Try this:
https?:\/\/(www\.)?[-a-zA-Z0-9#:%._\+~#=]{2,256}\.([a-z]{2,6}){1}
Try to replace www by something else:
/^(?:https?:\/\/)?(?:[^#\n]+#)?(?:[^.]+\.)?([^:\/\n\?\=]+)/im
EDIT:
If you absolutely want to preserve the www into your regex, you could try this one:
/^(?:https?:\/\/)?(?:[^#\n]+#)?(?:www\.)?(?:[^.]+\.)?([^:\/\n\?\=]+)/im
export const extractHostname = url => {
let hostname;
// find & remove protocol (http, ftp, etc.) and get hostname
if (url.indexOf("://") > -1)
{
hostname = url.split('/')[2];
}
else
{
hostname = url.split('/')[0];
}
// find & remove port number
hostname = hostname.split(':')[0];
// find & remove "?"
hostname = hostname.split('?')[0];
return hostname;
};
export const extractRootDomain = url => {
let domain = extractHostname(url),
splitArr = domain.split('.'),
arrLen = splitArr.length;
// extracting the root domain here
// if there is a subdomain
if (arrLen > 2)
{
domain = splitArr[arrLen - 2] + '.' + splitArr[arrLen - 1];
// check to see if it's using a Country Code Top Level Domain (ccTLD) (i.e. ".me.uk")
if (splitArr[arrLen - 2].length === 2 && splitArr[arrLen - 1].length === 2)
{
//this is using a ccTLD
domain = splitArr[arrLen - 3] + '.' + domain;
}
}
return domain;
};
This is what I've come up with. I don't know how to combine the two match rules into a single regexp, however. This routine won't properly process bad domains like example..com. It does, however, account for TLDs that are in the variety of .xx, .xx.xx, .xxx, or more than 4 character TLDs on the end. This routine will work on just domain names or entire URLs, and the URLs don't have to have the http or https protocol -- it could be ftp, chrome, and others.
function getRootDomain(s){
var sResult = ''
try {
sResult = s.match(/^(?:.*\:\/?\/)?(?<domain>[\w\-\.]*)/i).groups.domain
.match(/(?<root>[\w\-]*(\.\w{3,}|\.\w{2}|\.\w{2}\.\w{2}))$/).groups.root;
} catch(ignore) {}
return sResult;
}
So basically, the first routine strips out any potential stuff before the ://, if that exists, or just a :, if that exists. Next, it looks for all non-word boundary stuff except allows the dash and period like you'd potentially see in domains. It labels this into a named capture group called domain. It also prevents the domain match from including a port such as :8080 as an example. If given an empty string, it just returns an empty string back.
From there, we then do another pass on this and instead of looking from the left-to-right like you would with the preceding ^ symbol, we use the ending $ symbol, working right-to-left, and allow only 4 conditions on the end: .xx.xx, .xx, .xxx, or more than .xxx (such as 4+ character TLDs), where x is a non-word boundary item. Note the {3,} -- that means 3 or more of something, which is why we handle the TLDs that are 3 or more characters too. From there, we allow for a non-word boundary in front of that which may include dashes and periods.
EDIT: Since posting this answer, I learned how to combine the full domain and the root part into one single RegExp. However, I'll keep the above for reasons where you may want to get both values, although the function only returned the root (but with a quick edit, could have returned both full domain and root domain). So, if you just want the root alone, then you could use this solution:
function getRootDomain(s){
var sResult = ''
try {
sResult = s.match(/^(?:.*?:\/\/)?.*?(?<root>[\w\-]*(?:\.\w{2,}|\.\w{2}\.\w{2}))(?:[\/?#:]|$)/).groups.root;
} catch(ignore) {}
return sResult;
}

Class should be shown/hidden, depending on its content - but it only gets hidden

I'm working on a PHP-chat right now.
When a user logs in he automatically sends a message "logged in" that is shown to everybody, when he logs out he automatically sends "logged out" that is shown to everybody (except him of course).
When all users logged out, all messages are deleted automatically.
I'm now working on a function that tells you, whether you are alone in the chatroom or not.
I want to solve this using JavaScript. The script I use right now counts how often the term "logged in" and the term "logged out" appears in the chat history (yeah, not the ultimate solution, but absolutely sufficient for my use)
Here's the code:
function countverlassen(){
var temp = document.body.innerText;
// the g in the regular expression says to search the whole string
// rather than just find the first occurrence
var countverlassen = (temp.match(/verlassen/g) || []).length +1;
console.log(countverlassen);
}
function countbetreten(){
var temp = document.body.innerText;
// the g in the regular expression says to search the whole string
// rather than just find the first occurrence
var countbetreten = (temp.match(/betreten/g) || []).length;
console.log(countbetreten);
if (countbetreten >= 2 && countbetreten != countverlassen){
$('.alleine').hide();
}
else if (countverlassen >= 2 && countbetreten == countverlassen) {
$('.alleine').show();
}
}
The class "alleine" only contains the text "You are alone in here right now".
When another user logs in this should be hidden, when all other users except you log out it should be displayed again.
Hiding the class works fine, but it just won't reapper again when everybody else logged out.
You can see it in action here: http://team3.digital-cultures.net/index.php#
Just pick a name and chosse a start / destination from the dropdown.
What am I doing wrong?
Thanks!
EDIT: For testing purposes you can just enter "betreten" ("logged in" in German) and "verlassen" ("logged out in German") in the chat, no need to log in with multiple accounts :)
You need to use return to get a value out. Using the function name is only creating a new local variable. I've renamed the var to make this clearer.
function countverlassen(){
var temp = document.body.innerText;
// the g in the regular expression says to search the whole string
// rather than just find the first occurrence
var verlassenCount = (temp.match(/verlassen/g) || []).length +1;
console.log(verlassenCount);
return verlassenCount;
}
Use countverlassen() to call the function and get the value. Store the value instead of calling the function every time.
function countbetreten() {
var temp = document.body.innerText;
// the g in the regular expression says to search the whole string
// rather than just find the first occurrence
var betretenCounter = (temp.match(/betreten/g) || []).length;
var verlassenCounter = countverlassen();
console.log(betretenCounter);
if (betretenCounter >= 2 && betretenCounter != verlassenCounter) {
$('.alleine').hide();
} else if (verlassenCounter >= 2 && betretenCounter == verlassenCounter) {
$('.alleine').show();
}
}

script for on load alert box based on url

I am creating a submittal form, sending the form to a php form, and after the form completes having it redirect to the initial page with an additional "?s=1" in the url.
Basically what I am trying to do is create an alert box pop up on loading the page with the "?s=1" in the url.
It is a very brute force method to use I know, but i can't seem to get the small script to work correctly. I know for certain everything works and loads to the point and reloads the initial page with ?s=1 in it.
Here is the code i'm using to try and prompt the alert box
enter code here <script type="text/javascript">
var Path = window.location.href;
if (Path == "mywebsite.html?s=1")
{
alert("Your Form Has Been Submitted.")
}
else()
{
}
</script>
Does anybody know why the box will not appear? Or possibly an alternate method for what I am attempting to do? Thanks.
window.location.href contains the complete URL, including the domain, and the full path, so a basic equality comparison won't work unless you're exaclty matching it, and even still this could cause problems (e.g. www. versus a naked domain, https:// versus http://, etc.). A possible solution is to use RegEx.
var pathRegex = /mywebsite\.html\?s\=1/;
if (pathRegex.test(window.location.href)) {
alert("Your Form Has Been Submitted.")
}
As a note, you can have an if statement without an accompanying else, and else statements don't take any arguments in parentheses like if, unless you're talking about else if.
Here is some code I wrote up for one of my projects that lets you pull a parameter and value out of the url.
function GetURLParameter(urlParameter){
var url = window.location.search.substring(1);
var urlVariables = url.split('&');
for (var i = 0; i < urlVariables.length; i++){
var parameter = urlVariables[i].split('=');
if (parameter[0] == urlParameter){
return parameter[1];
}
}
}
It's easy to use:
For mywebsite.com?s=1
It would just be
var k = GetURLParameter('s');
if (k == 1){
alert("Your Form Has Been Submitted.")
}
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
And then check like...
if (getParameterByName("s")=="1")
{
alert("Your Form Has Been Submitted.")
}
else
{
}

regex detect url and prepend http:// [duplicate]

This question already has answers here:
Adding http:// to all links without a protocol
(4 answers)
Closed 8 years ago.
I would like to detect url's that are entered in a text input. I have the following code which prepends http:// to the beginning of what has been entered:
var input = $(this);
var val = input.val();
if (val && !val.match(/^http([s]?):\/\/.*/)) {
input.val('http://' + val);
}
How would I go about adapting this to only append the http:// if it contains a string followed by a tld? At the moment if I enter a string for example:
Hello. This is a test
the http:// will get appended to hello, even though it's not a url. Any help would be greatly appreciated.
This simple function works for me. We don't care about the real existence of a TLD domain to gain speed, rather we check the syntax like example.com.
Sorry, I've forgotten that VBA trim() is not intrinsic function in js, so:
// Removes leading whitespaces
function LTrim(value)
{
var re = /\s*((\S+\s*)*)/;
return value.replace(re, "$1");
}
// Removes ending whitespaces
function RTrim(value)
{
var re = /((\s*\S+)*)\s*/;
return value.replace(re, "$1");
}
// Removes leading and ending whitespaces
function trim(value)
{
return LTrim(RTrim(value));
}
function hasDomainTld(strAddress)
{
var strUrlNow = trim(strAddress);
if(strUrlNow.match(/[,\s]/))
{
return false;
}
var i, regex = new RegExp();
regex.compile("[A-Za-z0-9\-_]+\\.[A-Za-z0-9\-_]+$");
i = regex.test(strUrlNow);
regex = null;
return i;
}
So your code, $(this) is window object, so I pass the objInput through an argument, using classical js instead of jQuery:
function checkIt(objInput)
{
var val = objInput.value;
if(val.match(/http:/i)) {
return false;
}
else if (hasDomainTld(val)) {
objInput.value = 'http://' + val;
}
}
Please test yourself: http://jsfiddle.net/SDUkZ/8/
The best solution i have found is to use the following regex:
/\.[a-zA-Z]{2,3}/
This detects the . after the url, and characters for the extension with a limit of 2/3 characters.
Does this seem ok for basic validation? Please let me know if you see any problems that could arise.
I know that it will detect email address's but this wont matter in this instance.
You need to narrow down your requirements first as URL detection with regular expressions can be very tricky. These are just a few situations where your parser can fail:
IDNs (госуслуги.рф)
Punycode cases (xn--blah)
New TLD being registered (.amazon)
SEO-friendly URLs (domain.com/Everything you need to know about RegEx.aspx)
We recently faced a similar problem and what we ended up doing was a simple check whether the URL starts with either http://, https://, or ftp:// and prepending with http:// if it doesn't start with any of the mentioned schemes. Here's the implementation in TypeScript:
public static EnsureAbsoluteUri(uri: string): string {
var ret = uri || '', m = null, i = -1;
var validSchemes = ko.utils.arrayMap(['http', 'https', 'ftp'], (i) => { return i + '://' });
if (ret && ret.length) {
m = ret.match(/[a-z]+:\/\//gi);
/* Checking against a list of valid schemes and prepending with "http://" if check fails. */
if (m == null || !m.length || (i = $.inArray(m[0].toLowerCase(), validSchemes)) < 0 ||
(i >= 0 && ret.toLowerCase().indexOf(validSchemes[i]) != 0)) {
ret = 'http://' + ret;
}
}
return ret;
}
As you can see, we're not trying to be smart here as we can't predict every possible URL form. Furthermore, this method is usually executed against field values we know are meant to be URLs so the change of misdetection is minimal.
Hope this helps.

Categories

Resources