How to open a hyperlink on the same page with javascript? - javascript

I am using this to open on a new window:
<div class="backbutton" OnClick="javascript: window.open('http://www.goesback.com')">Go back</div>
How to amend this if I want onclick to open a hyperlink on the same page?
thanks.

You just set the url to the window.location.href
so
<div class="backbutton" OnClick="window.location.href = 'http://www.goesback.com'">Go back</div>
But you should really just convert it to a regular link
<a class="backbutton" href="http://www.goesback.com">Go back</div>

location.href='http://www.goesback.com'
Though you might want to reconsider using onclick to trigger that.

Why not just use a simple anchor tag?
Go There
If you need to use JavaScript, the other answers - setting the window.location property - will do what you want.

The second parameter of window.open() is a string representing the name of the target window.
Set it to: "_self".
<div class="backbutton" OnClick="javascript: window.open('http://www.goesback.com','_self')">Go back</div>

Set the window.location.href property:
<div class="backbutton" OnClick="window.location.href='http://www.goesback.com'">
Go back
</div>
Or just use a plain old anchor (probably your best option):
Go back
Note: the javascript: prefix is not needed in the onclick handler.

<div class="backbutton" OnClick="window.location.href = 'http://www.goesback.com'">Go back</div>

Related

Sharepoint trigger a javascript on anchor to remove attachment

I want to trigger the javascript "RemoveLocal("attachRow1","fileupload1")" on the anchor on code below,
<span class="ms-delAttachments">
<img src="/_layouts/15/images/rect.gif?rev=44">
<a href='javascript:RemoveLocal("attachRow1","fileupload1")'>Delete</a>
</span>
How can i do this?
You should use an onclick event to trigger the function call.
<span class="ms-delAttachments">
<img src="/_layouts/15/images/rect.gif?rev=44">
<button onclick="RemoveLocal('attachRow1','fileupload1')">Delete</button>
</span>
Problem solve.
$(".ms-delAttachments a")[0].click();

Can I use a <button> instead of <a> to go to #location?

I have this code.
<li class="active">
Weekly Payment
</li>
<li>
Advance Payment
</li>
<li>
Expenses
</li>
My question is that can I use <button> instead of <a> to achieve this? I changed it to buttons but they are not working.
Yes. Use <button type="button" onclick="window.location.href='YOUR URL HERE'">Link Text</button>
You can't use href in button but you can use data-href attribute to do this work. When button clicked get value of data-href and use window.location.hash to going to target id.
$("button").click(function(){
window.location.hash = $(this).data("href");
});
#first, #second, #third {
height: 200px;
border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button data-href="#first">First</button>
<button data-href="#second">Second</button>
<button data-href="#third">Third</button>
<div id="first">First</div>
<div id="second">Second</div>
<div id="third">Third</div>
You can try to take a look at this
Or you can create a button with a <a href> inside of it. I dont know wat you are trying to achieve with changing it into a button?
<button type="button">
hello
</button>
if it is for the style you can just apply a style to the a tag like this Go to Google
Goodluck!
Whenever you use #something in an anchor tag's href attribute it actually take you to an element in same page who's id is 'something'. You can use it like this as well:
click
In this case it will take you to the anotherpage.aspx page's element who's id is 'something'.
Now the purpose of button is completely different, but there are some ways to satisfy your requirement but that is not recommended. You should use anchor tag in this situation.
Here is a great link to show the difference between anchor and button tag. Check it.
Thanks.
Button does not have href functionality, so unless you use some JS functions to simulate this - No, you can't
You can use the styles over <a class="your-btn-style"> to show your anchor like a button.
If you are using bootstrap, you can simply add class="btn btn-primary" in your anchor for example :
Advance Payment
I also used this approach in my project :

jQuery initiate JavaScript action

<div class="button">
<a class="testclass" href="javascript:actionx(objectId);"></a>
</div>
So the question is how do I initiate JavaScript bit using jQuery, I have tried:
window.location = javascript:actionx(objectId);
And some other similar variations, but they do not seem to work.
By the way, class is universal and all the buttons have it. The only thing that changes is the objectId.
If you are trying to redirect then you can try this:
html:
<div class="button">
<a class="testclass" href=" javascript:actionx(1234);">Redirect</a>
</div>
script:
function actionx(data){
//rest of the code without return
window.location="d3.php";//here your location to redirect
}

Using href attribute for button element

I need that use href="" attribute for button
like this <button href="link">click me</button> Instead of <a href="link">
that's wrong .
I think that I've to try this code
<button onClick="FN('MY Link For Example www.stackoverflow.com')" >click me</button>
<script>
function FN('URL'){
--> Go to MY Link For Example www.stackoverflow.com
}
<script>
So what do u guys suggest I should do ?
Try this. This will navigate to the link on click.
<button onclick="window.location.href='http://www.stackoverflow.com'">click me</button>
Why you don't use <a type="button" href=""></a>
Or if you really want a button, so <button onclick="window.location.href=""></button>
I think this will solve your problem
<a href="http://www.stackoverflow.com/">
<button>click me</button>
</a>
href attribute can not be used on button tag , if you want button like appearance for your a tag you can use add style to your a tag and make it look like button , add bootstrap reference then add button class to your anchor tag.
Custom Bootstrap Button , but if you still want to use button tag then adding onclick event is better option

Javascript: Automatically clicking a button?

I'm learning how to write a chrome extension, and I'm fairly new to javascript.
Here's some html:
<div class="button data" style="">
<a class="button1 whiteColor" href="http://link1.com">VIEW This</a>
<a class="button2 redColor" href="http://link2.com">VIEW That</a>
</div>
What I want to do is open link2.com by automatically clicking button2 using javascript.
I'm using the following, but it's not working :/
document.getElementByClassName("button2 redColor").click();
Any help would be appreciated!!
document.getElementsByClassName("button2 redColor")[0].click();
You need select index, because getElementsByClassName return the array

Categories