How to remove 'http://' from a URL in JavaScript [duplicate] - javascript

This question already has answers here:
taking off the http or https off a javascript string
(13 answers)
Closed 6 years ago.
I have run into an odd situation. I'm writing a JavaScript bookmarklet that will allow users to click and share external websites to our website very easily and quickly. It simply get's the title, page URL, and if they've selected any text on the page, it grabs it too.
The problem is it doesn't work with external domains for some reason, so if we use it internally we end up with a share window with the URL formatted like this:
http://internaldomain.com/sharetool.php?shareid=http://internaldomain.com/anotheroddpage.html&title=....
That works just fine, BUT if we try to use an external domain and end up with a URL formatted like this:
http://internaldomain.com/sharetool.php?shareid=http://externaldomain.com/coolpagetoshare.html&title=...
Then we get a Forbidden Error on our page and can't load it... If we manually remove the http:// from the externaldomain address, it loads just fine again.
So.. I'm thinking the best solution to get around this problem is to modify the JavaScript bookmarklet to remove the http as it's loading the window. Here is how my current bookmarklet looks:
javascript:var d=document,w=window,e=w.getSelection,k=d.getSelection,x=d.selection,s=(e?e():(k)?k():(x?x.createRange().text:0)),f='http://internaldomain.com/sharetool.php',l=d.location,e=encodeURIComponent,u=f+'?u='+e(l.href)+
As you can see, e(l.href) is where the URL is passed.
How can I modify that so it removes the external domains http://?

I think it would be better to take into account all possible protocols.
result = url.replace(/(^\w+:|^)\/\//, '');

url = url.replace(/^https?:\/\//, '')

l.href.replace(/^http:\/\//, '')

I think the regular expression you need is /(?:http://)(.*)/i. The first match of this should be it.

Try using replace function
var url = url.replace("http%3A%2F%2F", "");

Related

How to pass any External link as a url paramenter using javascript? [duplicate]

This question already has answers here:
Test if links are external with jQuery / javascript?
(16 answers)
Closed 2 years ago.
I am totally new to javascript.
I have a site mysite.com, with tonnes of External links and I am trying to track every external link and append it as parameter to an external domain like
myanothersite.com/redirect.html?url=http://externallink.com`
using javascript.
myanothersite.com/redirect.html is a page which it redirects to passed url parameter after 5 seconds and i have created this page successfully.
In a nutshell, when a visitor clicks on external link, I don't want him to visit the external site directly. Instead I want to do this,
mysite.com
--> Visitor clicks on http://externallink.com
--> myanothersite.com/redirect.html?url=http://externallink.com
--> http://externallink.com
so, here I am trying write a js code that automatically detects external link and append it as a query parameter to
myanothersite.com/redirect.html?url=
there is a package on npm which may help you
qs
In addition, the. window object is a global object which can handle redirects, open a new window, or reload. So you can go through the documentations and red that one as well.
If you are looking for a simpler solution, you can try this
grab the link from the window object like this and store in a variable
var location = window.location.href
Then split it around the 'url=' like this
var splitArray = location.split('url=');
console.log(splitArray[1]);
You should see your url in the console. use it wherever you want

Parsing in JavaScript for chrome extension

I have a chrome extension that extracts all short url with the form e.g. ini/ini#8012 from any page using a regex.
var regex = /[\w]+.[\w]+#(?:\d*\.)?\d+/g;
What I want to do is to make that short url into a clickable link in my popup window, and parse it into my web app, so clicking any short url in the list would take you to a web app. The web app url is like this
http://192.101.21.1889:8000/links/?user_repo=ini%2Fini&artID=8012&tc=4&tm=years&rows=5&submit=
The user_repo, and ID characters are from the extracted short url. First of all, is this possible? And if it is can anyone point me in the right direction as to what to do?
You can use Content Scripts and inject JavaScript, then perform whatever you want to do.
Readings:
Content Scripts

what's an alternative to make dynamic links (other than javascript) to make them google-crawable? [duplicate]

This question already has answers here:
How is Github Changing Pages and the URL so smoothly without AJAX?
(2 answers)
Closed 9 years ago.
I have a page where in some links are built when the page loads using a javascript function makes link, depending on the current url of the page. On click, an ajax call loads the new page.
How do I make these links google-crawlable (since google doesn't crawl javascript links)?
As an example I'd like to mention github. So when you open, say, https://github.com/pennersr/django-allauth all the links inside are already loaded with their respective links, depending on the current URL. When you view-source, you can see the links there. Whereas you just see a javascript function in the view-source had the links been made through javascript. I don't think these values are being passed from the back-end as well.
What's a possible solution to do this?
This is a common issue in Single Page Applications or applications that use intensively JavaScript and AJAX.
First of all you need to create unique URL for this actions in JavaScript, so the crawler can at least "hit these actions". If you execute a function in JavaScript, but your URL doesn't change, Google will never be able to know that there's something happening there.
Normally AJAX URL's are written like this:
http://www.foo.com!#jsAction
Google crawler will be able to crawl this URL but probably the page that will get back will be blank since is the JavaScript code the responsible to render all the content.
This is why the crawler will change the '!#' for the word _escaped_fragment_ when calling your server. So the previous URL being requested by the crawler would be:
http://www.foo.com?_escaped_fragment_=jsAction
With this new keyword in the URL we can determine in the server that the request comes from a crawler, and here is when the magic starts.
Using a headless browser like PhantomJS we can execute the JavaScript code in the server and return the fully rendered HTML to the crawler request. This is one of the approaches that Google suggest in their guidelines.
So basically the point is to determine which type of request you get and execute different code depending if the query string contains _escaped_fragment_.
This link from Google might help you to point you to the right direction: https://developers.google.com/webmasters/ajax-crawling/
Hope it helps!

How can I customise all assignments to window.location.href?

In my JavaScript application, we have multiple places where we have used window.location.href="any string";. Now I want to write JS code in only one place (probably using window.location.prototype) to override assignments to href, so that I can append a parameter to all instances.
I want to append a parameter (e.g. "?abc=1234") to all urls which are assigned to window.location.href.
I want to write code that means when e.g.
window.location.href = "abc.html";
is written, it should actually result in the href being set to abc.html?abc=1234.
window.location.href = window.location.href + "?abc=1234"
I just test this in WebKit DevTools/
You can't actually do this.
It's more of the JavaScript engine which runs the page. All records in most browsers which are stored of your browsing is the history. Hardly anything else. So basically to the browser, there is no difference between a meta redirect, a header redirect and a javascript redirect.
Unless I'm wrong.

Concatenating pathname with a virtual URL for Tracking PageView

I'm using the Contact Form 7 plugin for Wordpress.
It allows you to put in a single line of javascript in the additional settings to trigger some javascript code when the send button is pressed.
My original code looked like this, which works fine:
on_sent_ok: "_gaq.push(['_trackPageview', '/CallbackFormFilled']);"
The "On_sent_ok:" is a specific parameter for the contact form.
I have a requirement where i need to concatenate the pathname of the URL together with the virtual URL i have created in the code above.
I tested concatenation of strings using 'Alert' to make sure i was doing it correctly, but when using the following code my pageview tracking fails
What is the correct syntax for concatenating the strings correctly here?
on_sent_ok: "_gaq.push(['_trackPageview',window.location.pathname+'CallbackFormFilled']);"
Try:
"_gaq.push(['_trackPageview',"/"+window.location.pathname+'CallbackFormFilled']);"

Categories

Resources