How to use `document.getElementById("a_link").click();` - javascript

So, I need to use document.getElementById("a_link").click(); in the website but I am not sure where or how to place it.
The setting that I have is that there is a submit button and link (http://demodemo.com)
I am trying to redirect users to the link when they press submit button.
I was told that document.getElementById("a_link").click(); will do the job.
But I am not sure how to do it.
Any help will be appreciated.
Thanks

Have your submit button and link look like this:
<button onclick="redirect()">Google.com</button>
Click the button instead fool
Then, in your javascript, have this:
function redirect() {
document.getElementById("a_link").click();
}
However, a much more eloquent way would be something like this:
<button>Click me for google.com</button>
This is simply a button inside of a link, making it so when you click the button, it will redirect you to google.com (or any other page of your choosing).

Related

How to load javascript after click in to another url

How can i add this JS into a link:
Im in page1.php;
I need click in some link Link
And when i go to page2.php some code load this JS:
javascript:void(lz_chat_change_state(true,false));
Any help?
It seems to be simple but not for me......
Solved !
If page1 has ?chat i execute the javascript in page2;
Link in page1.php?chat
In page2.php
if (isset($_GET['chat'])) {
include_once("js-externo.php");
}else{
// Fallback behaviour goes here
}
In file js-externo.php i put:
window.onload = function() {
void(lz_chat_change_state(true,false));
}
Do you mean you want to call that function when you click on the link?
If yes
Make javascript file seperate.
Inside the link you put a "onclick" event and call the javascript function as:
<a href="page2.php" onclick = "lz_chat_change_state('true','false')">
If you want things to change after going to page2.php particularly then you need to use sessions. Pass a value from page1 to page2. But looking at your function (which I assume is being used as a toggle) it's not necessary.

How can I force a button to do some action automatically?

Problem
I have a form with different stages. Image below in which I have multiple buttons(prev, next) and a submit button(to submit the form). I want to take the person directly to 5th stage (summary) with pre-populated data when they comes to this page.
I am using below mentioned javascript code but no success. I know the reason why no success and that is because in javascript I'm doing action on submit button whereas I want to do it on a normal button, which looks like this.
Can anybody guide my through the syntax of JavaScript in terms of how should I make an action only on the below mentioned button.
Button
<button type="button" class="next" onclick="loadnext(4,5);"><img src="images/next.jpg" alt="" /> </button>
JavaScript
<script type="text/javascript">
document.forms.login_form.submit();
</script>
Loadnext function
function loadnext(divout,divin){
console.log(divout + " -- " + divin);
ch=validateme_form(divout,divin);
if(!ch){return false}
//alert(ch);
jQuery("." + divout).hide();
jQuery("." + divin).fadeIn("fast");
}
document.forms.login_form.submit(); does not trigger the click event of your button, trigger it directly (sth. like jQuery("button.next").click()), but be sure to wait, that the dom is ready. (jQuery(function() { ... })
OK, you have a form and you collect values from showing and hiding some divs and you want to post the form at the end as I can see.
Place the loadnext function inside a in section.
Since the button is not submit leave it as it is, or if you like it to serve as submit also change the onclick to onclick="loadnext(4,5); return false;"
I do't know what validateme_form does, but I think it will work.
make sure all elements have the correct ids as needed.
Hope this helps.

jQuery link confirmation

I am implementing a form which has got links in it,
like this:
<form>
<a>FAQ </a> /* something this way */
<submit button>
</form>
I have to display a confirmation box to the user, if the link is clicked and only when it tries to load the href. In case the user is opening the link in new tab or uses 'CMD-Click' (Mac), the prompt must not be shown.
In Firefox, the browser itself takes care of this when the user tries to navigate to another page when he is in the middle of the form, but I need this functionality to work in all browsers.
Does anyone know how to do this?
that's simple
Google
but i'm not sure if CMD+Click doesn't alert the user. Most of these event can't be controlled by javascript as they are coded into browsers.
Maybe something like this? demo
<form>
FAQ /* something this way */
<submit button>
</form>
$("a").click(function() {
if (!confirm("Do you want to leave?")) {
return false;
}
});
As for not displaying on new tab/new window, there are no such javascript events, thus you cannot capture them.

How to disable back,forward and Refresh functionality in browser?

Can anyone help me to disable browser back,forward, back button and right click menu functionality using JavaScript or JQuery? I have tried disabling the back button like this:
function disableBackButton() {
window.history.forward();
}
setTimeout("disableBackButton()", 0);
I have called this function in the Body onload event. I have a doubt that where we need to put this code in the Calling page or Called Page.Suppose i have two pages like this FirstPage.aspx and SecondPage.aspx. When I navigate from the First Page to Second Page when I click back button of the browser it should not go to FirstPage.aspx.
Have a look at A Thorough Examination of "Disabling the Back Button."

How can I implement a confirmation dialog for critical action?

Im not proficient in JS myself very much, so it would probably take me a few hours to figure this out myself, so I figured I'd ask you guys.
I have Delete/Ban links on my site, that are used by admins to moderate the users and what they post. Since these links have such drastic consequences on the site's content I would want to have some sort of confirmation that would popup when one of the links is clicked.
It should have some extra text explaining what's about to happen, and OK/Cancel buttons.
So I have a link, for example like this:
Ban John
Delete Post
When this link is clicked, the code needs to be triggered. If OK is pressed, it goes to ban.php?id=123, if Cancel is pressed, it doesn't go anywhere.
A simple JS dialog box, will do. Or if you have a link for a fancy jquery powered box, you can point me towards that also.
Thanks!
Easiest way to achieve this is to use the confirm() function:
var confirmed = confirm('Do you really want to do this?');
if(confirmed) {
//Do dangerous action
}
It basically displays a box with the text you provide and ok and cancel buttons.
With a link you can do something like this:
<a href="someurl" onclick="return confirm('Are you sure?');">
It's not best possible practices to put the action in the onclick like that, but it works and is simple to understand even if you're not a pro javascripter
I think you're looking for something like "confirm delete".
The Confirm function takes as an input a string parameter. It displays a dialog box whose message is that parameter, with OK and Cancel buttons.
If the user clicks OK, the confirm function returns True; if the user clicks Cancel False is returned.
Here's an example of using it:
function fConfirmDelete()
{
if(confirm("Delete Field?"))
{
document.forms[0].lblFieldSelect.value="DELETE";
document.forms[0].submit();
}
else
{
document.forms[0].lblFieldSelect.value="";
}
return;
}
Here's a decent jQuery dialog plugin. Of course, you will need jQuery too.
http://nadiana.com/jquery-confirm-plugin
var confirm = confirm('Blabla');
if(confirm)
{
// do
}
This is plain JS:
<a href="delete.do"
onClick="return confirm('Delete. Are you sure?');">Delete</a>
Looking for this?
//Cleaner explanation
$("document").ready(function(){
$(".dangerousLink").each(function(){
var confirmUrl = $(this).attr('href');
$(this).attr('href', 'javascript:;');
$(this).click(function(){
if(confirm("This is dangerous... are you sure?")) {
window.location = confirmUrl;
}
});
});
});
//Now just append a class dangerousLink to all links that you want confirmation
Blow up
If you are looking for a fancy dialog, you'll find something interesting here.

Categories