how can i open a link without using "http" info..? - javascript

In my app, I have a field to fill by my user for their URL. After they fill I convert the url into a href link. But sometimes user does not add a http:// prefix to their link, so a link is not working properly..
Is there any way to open a link even without the http:// prefex in the href?
If anyone knows the way without using javascript is fine, else we can try with javascript.
Here is my
<a target="_blank" href="http://www.google.com">Google</a>
<br>
<a target="_blank" href="http://google.com">Google</a>
<br>
<a target="_blank" href="www.google.com">Google</a> // this is not work!
Live Demo

You should force the user to insert a valid url, for instance by using a proper inpout tag:
<input type="url" placeholder="Enter a valid URL - e.g. http://path.to/website">
In case you can't/you don't want to edit existing content
$('a[target="_blank"]').click(function(e){
e.preventDefault();
window.location = /^(http|https):/.test(this.href) ? this.href : 'http://' + this.href;
});

Related

How can I replace Current page with another page then open current page in another tab

Hello my Wonderful Developers, I need help on this
Let say my website is https://nkiri.pw/ and I want to click on a button or Link, I want my current page to be replace by this page https://itinerarycarter.com/qm37kgvszd?key=7f255a06003c7158485ad05f1e9672f3 and my present page should open another page automatically. How can I achieve this successfully ?
I was able to solve it by doing this:
<a role="button" id='mike' href="<?php echo "https://".$_SERVER['SERVER_NAME']."/" ?>" target="_blank" class="btn btn-primary">Primary</a>
<script>let linkElement = document.getElementById("mike"); linkElement.addEventListener("click", openNew);function openNew() {window.open("https://itinerarycarter.com/h9xdx19n?key=2053deb616c8b3ac8566b44a8ec7c092", "_self");}</script>
You are using a hyperlink. So you have a href="" in your html tag. You can also set the target (where the page should be opened) to _blank. This means, that the page will be opened in another tab. So if you write it like this:
href="https://nkiri.pw/" target="_blank"
It will open your current page in another tab. Then you can open the new page with js.
let linkElement = Document.getElementById("yourIdOfTheLinkElement");
linkElement.addEventListener(function() {
window.open("https://itinerarycarter.com/qm37kgvszd?key=7f255a06003c7158485ad05f1e9672f3", "_self");
});

Update not working for href property in Markdown

I want to create a custom hyperlink which passes current URL as one of the parameter to web app endpoint (abc.azurewebsites.net).
These are the versions I tried with thought process of getting the element by Id and update the href using basic HTML properties but no luck.
Approach 1
> <script> baseurl="abc.azurewebsites.net?="
function buildURL(item) {
> item.href=baseurl+window.location.href;
> return true; }
</script>
> </head> <body> <a onclick="return buildURL(this)" href="">Google</a> </body>
Approach 2
Click on this <a href=""
target="_self" id= "test" onclick = buildURL() >Link </a> to execute
<script>
var el_up = document.getElementById("test");
function buildURL() {
this.href = "abc.azurewebsites.net?=" + document.URL;
}
</script>
Any suggestions please. Is it even possible?
Update: Just to mention, I'm trying this from Azure Devops description section
I believe this will work:
<body>
<a id="link-to-edit" href=""> Click here to execute </a>
</body>
<script>
baseurl="https://abc.azurewebsites.net?="
link = document.getElementById("link-to-edit")
link.href = baseurl + window.location
</script>
This will use javascript to programmatically update the href to the baseurl plus current location as soon as the page/script loads. Then when you click the link will be executed properly. The key is that the href must be set at the time of clicking. In your examples, you set the href AFTER the user clicks and thus the link will not work as you intend. Hope that helps!

Can't open PDF on new tab using target="_blank"

How do I open PDF on a new tab, target="_blank" alone does not work it still open the pdf in the same tab.
Method-1 : HTML
<a target="_blank" href="http://your_url_here.html">Link</a>
You can simply do that with setting target="_blank" for an example check this
More Details
Method-2 : Javascript
<a onclick="openInNewTab('Your URL');">Something To Click On</a>
function openInNewTab(url) {
var win = window.open(url, '_blank');
win.focus();
}
Without your code is hard to tell what's wrong but did a fast test and this worked for me..
<a target="_BLANK" href="pdf/your_pdf.pdf">YOUR PDF</a>
You have to know: "_Blank" is not working as a "new tab" on every browser.
To do that, you have to use js like this:
Lnk
(it will work on any browser, "_blank" will not)
EDIT: Of course, here the "link" in window.open will be the path to where your PDF file is stored.
EDIT2 (thanks to vlaz): Yep, it will work on any browser if JS is enabled, if he his not it will not.

HTML: How to make page keep on the "href" page, and open a new page for "onclick"

I tried to fix hard code(web link in href) in the following line:
'<a class="top_a" target="_blank" href="http://10.74.55.99:3000/"><img src="images/load.svg"><img src="images/nbsp.png">LoadServer</a>'
modified to:
<script type="text/javascript">
function openLoadserver() {
window.location.href = load_server; //defined load_server in other file
}
</script>
'<a class="top_a" target="_blank" href="#" onclick="openLoadserver()"><img src="images/load.svg"><img src="images/nbsp.png">LoadServer</a>'
The result is, after I click "LoadServer", current page will open the load server page, and a new tab page open the "current page" again.
My expected result is current page is keeping as original, new tab page will open the load server.
What should I do?
<a class="top_a" onclick="openLoadserver()"><img src="images/load.svg"><img src="images/nbsp.png">LoadServer</a>
Notice the difference, the target attribute :) You don't need href either

Facebook share with javascript

I am using html javascript to share a link on facebook.
I am using this code:
<a href="javascript:"onclick="window.open('https://www.facebook.com/sharer/sharer.php?u=test.com');" target="_blank">
Share on Facebook
Now i want to replace test.com with the current url link in the browser .
How to do it !
You can use window.location or window.location.href which provides the current URL. In your example, you can use something like the following:
<a href="javascript:" onclick="window.open('https://www.facebook.com/sharer/sharer.php?u=' + window.location);" target="_blank">
For more information see Window.location on Mozilla.org which has the following description:
The Window.location read-only property returns a Location object with information about the current location of the document.
Though Window.location is a read-only Location object, you can also assign a DOMString to it. This means that you can work with location as if it were a string in most cases: location = 'http://www.example.com' is a synonym of location.href = 'http://www.example.com'.
I believe it should work if you use window.location.href, so your code would be the following:
<a href="javascript:"onclick="window.open('https://www.facebook.com/sharer/sharer.php?u=' + window.location.href);" target="_blank">
window.location.href will get the current url e.g.
window.open('https://www.facebook.com/sharer/sharer.php?u=' + window.location.href);

Categories