How to complete a link with user input - javascript

I'm completely new to javascript, and I'm trying to get a script to complete a link with a user input.
I.e. I have a common base url like http://www.mystuff.com/ and I would like to create a form to let the user reach a specific page if he knows the exact url. I need a basic input field where he can write a string (like h5mlf4) that will send him clicking the submit button to http://www.mystuff.com/h5mlf4.
I've found this script on this page which is pretty close to my needs. I've tried to modify it, but I don't know how to do both the actions (add the user's input and launch the link) on the same button with an all-in-one action. I don't want to create the link on the page, I simply need it to be immediately used.
I hope to have been clear, and thanks to everybody will take care of this.

Do
window.location = "http://www.google.com?q=" + userInput;
to "click" that link (i.e. go to that link) for the user.
Doc for window.location >> https://developer.mozilla.org/en-US/docs/DOM/window.location

Related

Saving URL to cookie and adapting to button

I'm currently looking for something to do the following:
scenario:
user clicks button on page 1-15 and navigates to page 16, user does several actions, and when clicking on a 'confirm' button, he needs to return to page 1-15 (whichever he was on earlier).
How could I do this? I was thinking to simply write the URL in a cookie which can then be read out and adapted in the final destination URL.
Or am I looking in the wrong direction? Some code examples would be helpful as knowledge is limited.
use sessionStorage.setItem('currentPage',1);.
then when you need to use this number just call sessionStorage.getItem('currentPage')

Creating a form that directs users to specific URL based on results

I need to develop a screening survey that directs the job seeker to a specific URL based on their answers. At the end of the screening it will say "Based on your answers, we think the following position is the perfect fit for you" and it will list that position. The user will then click "submit" which will re-route them to the application for that position.
I also need the survey, with the answers recorded, emailed to an address for HR when the user clicks "submit" and is routed to the application.
What is the best way to achieve this? How can I get started?
A simple way to change the action of a form, is to use jQuery to change it based upon your results, and then submit will achieve your results.
If the results are returned, via another page (i.e. the submit posts to another page), you may want to redirect via meta tag.
Generally, if you are redirecting after returning the results, both a meta tag, with a suitable timeout and a link that the user can click would be appropriate.
This example may be instructive assuming a form with an id="selectsearch":
$("#selectsearch").change(function() {
var action = $(this).val() == "people" ? "user" : "content";
$("#search-form").attr("action", "/search/" + action);
});

Google Tag Manager Form - Thank You Page

I am trying to use google tag manager to have a tag that will fire every time the user submits the form and lands on the thank you page. In this scenario, I know an option is to use the Trigger: Page View, and then specify the thank you page URL in the conditional statement.
However, since this URL can be shared, I only want to track when the user submits the form and lands on the thank you page (not when the URL is accessed through other ways). What would be the best way to tackle this?
Why would people share the thank-you page? I can imagine sharing a form, but why would the url of the Thank you page get shared.
However, what you can do is track clicks on the button or link that submits the form.
I ll advise assuming you NEED to fire when the user gets to the ThankYou Page for X reason and CANT be done on the button submit click.
Add a custom HTML tag with a trigger on the click of the form submit button (you can use build in variables such as Click Classes) and add a 'flag' on the session storage or a cookie (choose the one you are more comfortable with).
Then generate a trigger that fires on the Thankyou page AND has the flag on.
Example:
Tag when user submit the form:
<script>
storage.setItem('flagSubmit', 1);
</script>
Custom JS variable that checks if user has flag:
function(){
return storage.getItem('flagSubmit');
}
If you need any more help just ask.
Hope it helps!
-- EDIT: add info
As you comment you cant detect if the flag is being saved properly so here is a code you can paste on the console that ll avoid you browser to exiting the page and allowing you to see if your click tag worked and registered on session storage you data.
window.addEventListener("beforeunload", function() { debugger; }, false)

How to store URL of clicked hyperlink

I am displaying a warning dialog box whenever user tries to navigate from current page without saving data on current page. Its working fine now I want to call a function (Spring controller, its kind of java function which handled URL mappings ) when user clicks on Ok (in warning dialog box) and then he should get redirectd to desired page.
Let me try to make it simple (Its confusing for me also):
User is on registration page, he/she made some changes and didn't save it.
Now user clicked on any other link for example he clicked on About Us link.
Now I want to execute my spring controller.
After execution of controller user should get navigated to About Us page.
For this I need to save value of clicked hyperlink and pass it to my spring controller.
So how can I store URL of clicked link (URL of About Us page in above example) ?
PS: I will really appreciate if anybody can edit my question to make it easier to understand.
if you use jQuery, you can attach an event handler to onclick to all links in the page and once clicked the handler should save the href attribute to some variable. then create a onbeforeunload event listener on your window, where you can use the value however you want, call your controller or save the value in a cookie or something.
Are all the links on the page pointing to your spring application? If there are no external links anywhere (pointing to external resource) - then you could write a simple Filter where you can save the requested page into the session.
Otherwise, if there are links to external resources - you would need to rewrite them from www.external.com to www.my.com\MagicController?requestedPage=www.external.com. Controller will save the link and send a redirect in HTTP header to the requested page. This is a common practice - even google does that (check out the google search result links for how it will look like).
Added: Weird, but google does that only on some rare occasions, so you probably won't be able to find an example there.
Don't require to preserve the href of selected tab.Do one thing attach same javascript function with each tab and pass the "this" as parameter of function.
Function of the javascript is
function Attach(ele)
{
// 1. Find the handle of selected tag and store in the variable.
ele=$(ele);
// 2. Find the value of href
var href=ele.attr("href");
// 3. Perform server side operation you want.
// 4. redirect to another page.
window.location=href;
return false;
}

Sharepoint: Submit form data from Edit form and then redirect to display form

This is for a custom Editform.aspx being used to submit data to a custom list. Once the user completes the form, he should be able to hit a button or even the OK button, and have sharepoint submit the form data to the list, then redirect to the items Display form (dispform.aspx?ID='itemid').
I was able to get the ID of the current item by manipulating document.location.href. I tried to do a postback in javascript using __doPostBack with __commit and __redirect (redirecting to the URL displayform?ID='itemid'), but when the redirect happens, I see that the changes were not updated!
Anyone able to do something like this?
You can get the form to redirect to any location after it's been posted by modifying the URL in the Source querystring parameter. Using javascript, you could look for a specific URL (/location/dispform.asp?ID=) in that querystring parameter and if it doesn't exist, redirect the page to itself, but with the Source parameter filled out to the location w/ ID that you want to send them to.
The hardest part will be parsing the current URL + parameters in javascript. There's some snippets of JS code I've found that make it a lot easier.
One of the bonuses of rewriting the EditForm URL this way is that the Cancel button will send the user to the DispForm page instead of where they originally came from, although most people probably use the back button.
This lead me to solve my problem of a Form Action on my DispForm that would not take the list item ID to the custom EditForm. The following ended up working for me:
onclick="javascript: {ddwrt:GenFireServerEvent(concat('__redirect={ProgEditForm.aspx?ID=+',$ListItemId,'}'))}"
Where I have a DispForm parameter for ListItemID with
Parameter Source: Query String
Query String Variable: ID
Default Value: 0
Hope that helps someone else as this was near driving me crazy!

Categories

Resources