Href only works with http - javascript

I want to redirect to a different page when clicking on my H2.
The thing is it only works if I have http on the code.
<h2 id="btn_share">Share...</h2>
<script type="text/javascript">
document.getElementById("btn_share").onclick = function () {
location.href = "http://www.google.html";
};
</script>
I have the google link as an example. What I really want is to redirect to a local page, therefore I cannot use the http://. But it does not work. But also, if I just write "www.google.com" it does not work. It only works with http://
Why? And how to fix it?
(I am using Microsoft Visual Studio)

You should use a local path.
Here's how it works:
http://www.google.com or //www.google.com will give you Google's website, because the // tells the browser to use the protocol of whatever current page is (http://, for example). Obviously, you can specify it yourself.
If you prefix the link with a single /, it will start from the root of the current domain. For example, from http://www.example.com/example/example2.html with a link to /about would bring the user to http://www.example.com/about
Excluding the / or using a ./ will search the local directory. For example, from http://www.example.com/example/example2.html with a link to about would bring the user to http://www.example.com/example/about
Hope this helps.

Related

How do I forward my page in Javascript if the website can't use "https://"?

So I have a button that will forward the page to a website like this example.tk, but it keeps on thinking that I just want to go to ./example.tk inside the website folder where it is originating from.
I've tried window.location, window.location.replace, window.location.href,
and self.location.
myButton.addEventListener('click', function() {
window.location.href = "example.tk";
});
I want the website to change from the url it is to example.tk, how would I do that if the example.tk does not have a certified https tag?
I want the website to change from the url it is to example.tk, how would I do that if the example.tk does not have a certified https tag?
It's not quite clear what you mean there. If you mean you want to link to an https:// resource from an http:// resource, just be explicit:
window.location.href = "http://example.tk";
// ---------------------^^^^^^^
If you mean, you want to use the same protocol as the current page (http: or https), you can use a protocol-relative URL:
window.location.href = "//example.tk";
// ---------------------^^
That will use whatever the protocol of the current page is. On http://example.com, it will link to http://example.tk. On https://example.com, it will link to https://example.tk.
Hover the link in this snippet to see it in action (no need to click):
hover this link

IE Linking to local files with spaces works but! not any more sub folders

Thought I would edit this as my original question has somewhat changed.
Originally I wanted to know how to open local folders with a space in them, I have this working now by doing this:
The link:
<a class='filelink' href='#' alt='file://\\\\TIT01\\Titan Power\\'>LINK</a>
The JS I use to open this link:
$('.filelink').click(function( event ) {
event.preventDefault();
var location = $(this).attr('alt').replace("%20", / /g);
console.log(location);
window.location = location;
});
To get it working I had to add the website as a trusted site to IE and this then allows linking to local files but you also need to accept a popup which asks if you want to allow the local site to open local links which you just say allow to.
The problem I have now is that if you add anything more on to the file link it fails to find the file so this link fails:
<a class='filelink' href='#' alt='file://\\\\TIT01\\Titan Torque\\Jobs\\'>LINK</a>
The folder is valid, the other weird thing that's happening is I get no link in the log like it will open the error message saying "Cannot find 'file://tit01/Titan%20Power/Jobs/" but I don't understand why the %20 is even in that error unless it encodes a URL for the error message.
Any help would be really appreciated.
OK got it working.
<a class='filelink' onclick='javascript:window.open("file://TIT01/Titan Power/Jobs/2014/TTS14001");'>Link</a>"
Also other things I had to do were add the website to trusted sites which is not ideal for most people I take it.
Also the first time I clicked the link it asked if I wanted to allow Internet Explorer to open the local file.
Wow that was 2 days of headaches!
Don't set the path with the alt attribute;
The %20 character is normal. All browsers automatically encode spaces to %20;
Try setting the anchor like so:
<a class='filelink' href='file://TIT01/Titan Power/Jobs/'>LINK</a>

Get URL in javascript without file extension

In Google Analytics I'm tracking goals with virtual page views. I take
trackingURL = window.location.pathname+'thankyou.php';
and then
_gaq.push(['_trackPageview',trackingURL]);
The issue is when a page is something like www.domain.com/page.php it ends up being domain.com/page.phpthankyou.php and not tracking properly, but if it was domain.com/page/ and then it became domain.com/page/thankyou.php it would track properly.
How can I get the full url, without the extension, so I can add on /thankyou.php, but if it is already a directory with the / at the end, then I just want to add thankyou.php to it?
Thanks!
Use this (which only replaces the extension with a slash if it finds it):
trackingURL = window.location.pathname.replace(".php","/") +'thankyou.php';
Which replaces domain.com/page.php with domain.com/page/

Javascript redirection / domain specify

I found an nice script while searching and inspecting the elements of some websites.
This is what I have found:
<script type="text/javascript">
//redirect browser to fullscreen preview
if (/^http:\/\/codecanyon\.net/.test(document.referrer))
window.top.location.href = 'http://www.gravitysign.com/backslider/';
</script>
So if I understood from this script it tells jquery if the website is opened over codecanyon redirect them to specifed website for preview.
Now... I was wondering if there is possibility to make something like this.
If we specify an website for example http://google.com and we input that into javascript... And then if that website is uploaded to any other domain, other then google.com ... It will redirect to specified site (google) ?
So to clear things out a little bit let me make an example.
If I made a website for "an-website.com" and then someone take their website and upload it to "another-website.com", it will automatically redirect all visitors from another-website.com to an-website.com.
Hope I was clear enough and hope that this is possible. Cheers!
You can of course redirect any user accessing your site from a domain not matching yours but using javascript. This should work just fine:
if (window.location.hostname !== 'yourdomain.com'){
window.top.location.href = 'http://yourdomain.com';
}
You can also use match, if you host your site on a subdomain, etc.
Keep in mind that any person with write access to the file on the server will be able to remove this "copy protection". Copy protecting client side content is impossible, as you need to serve the content in a way a browser understands, effectively making the content available to anyone.
If you are looking for solution for single domain protection, here you can see my
Redirect Website if its not specified domain in script - Protection using Javascript
I am looking for solution for multiple domain.

How to automatically replace `url` in browser address bar with JavaScript: from /en/services/ to /en/#services?

How to automatically replace url in browser address bar with JavaScript
from company.com/en/services/
to company.com/en/#services
?
Example: when I type in browser address bar url company.com/en/services/ and click 'Go', it will be automatically seen company.com/en/#services
Is there any way to replace real url /services/ with hash url /#services without browser refresh and no redirecting? Does jQuery has some solution for that?
You can't change the URL with Javascript for the current page. You can only change the hash like this (without causing a refresh):
window.location.hash = '#services';
So when you're at the page company.com/en/ and then click something, you could then set the window.location.hash. For example, it could be changed to company.com/en/#anything_you_set. The only other way is to do what Pekka suggested and reload the page.
If you want them to type the url and have it change to the hash, you're going to have to look up URL Rewriting (at least for ASP.NET and IIS). If you're on IIS7, you can use the URL Rewrite Module.
If you're on apache, you can read this URL rewrite tutorial.
You could do something like this:
$(document).ready(function() {
window.document.location.href = 'company.com/en/#services'
});
I'd go with:
<script>
document.replace("/en/#services");
</script>
<meta http-equiv="refresh" content="0;url=/en/#services" />
on /en/services/
Document.replace(url) will have the browser load the new page, and the old one won't be in the history, so when the user hits back, they won't get stuck in loop. The meta catches people without JS.
I don't think that you can't reliably do this with a server-side redirect, as many browsers (and the HTTP spec) consider the hash client-side only, and so it doesn't survive the redirect.
I found the solution in http://www.asual.com/jquery/address/.

Categories