Using hash value in photo gallery - javascript

I try to create a navigation for my photo gallery using hash values.
When I click the "Next" link, I get value of the hash and then post it to the another backend php script using jquery $.get() api and also set the new hash value for that link. Everything works fine but the hash doesn't update itself with every click. It usually takes 2 clicks to get the updated hash value.
Can anybody here tell me on how I could go about fixing it.

Update hash manually, using
location.hash = '#new_hash';

Related

How to Replace the path name of URL using JavaScript/jQuery?

I am trying to replace the url and load the page.
Example:
URL: http://www.example.com/account/edit
I want to replace this as http://www.example.com/account/add. And load this page.
For that, I tried by using location.pathname.
location.pathname = "/account/add";
When using like above, the add page is displayed. But after that, URL http://www.example.com/account/add is loading. Then the add page is not displayed. I get that page is not found message.
The host name should vary based on running the project. So I want to resolve this without based on host name.
How can I achieve it?
If you want to retain the page in your session history (meaning you want to be able to use the back button to it) use the assign method. Otherwise, you replace.
Replace the current document with the one at the provided URL. The difference from the assign() method is that after using replace() the current page will not be saved in session history, meaning the user won't be able to use the Back button to navigate to it.
http://mdn.beonex.com/en/DOM/window.location.html
window.location.href = "http://www.example.com/account/add";
OR
window.location.replace("http://www.example.com/account/add");
This will redirect to the new page.
Actually you can change your url some other ways like below:
window.location.assign("http://www.mozilla.org"); // or
window.location = "http://www.mozilla.org";

Best way to pass a single-use value between requests using JavaScript

I need to trigger a page reload, via JS, preserving the vertical scroll position.
I'm using the solution described in another SO question:
Calculate the current scroll position
Add the current position as a querystring argument, then redirect
When the page reloads, read the value from the querystring & adjust the scroll pos
However, I only want to restore the scroll position on that FIRST redirect. If the user scrolls around the page and then triggers a manual reload using Ctrl-R, I do NOT want to re-scroll to that saved position.
Is there some way of passing a single-use, visible-to-the-next-request-only value using ONLY JavaScript? Or from removing a value from document.location.href without redirecting?
Should I be using the HTML 5 History API to "clear" the position value after I've consumed it?
Save the value to sessionStorage. Once you use it, delete the value so it cannot be read on a manual refresh.
sessionStorage.setItem("scroll_position", "300");
sessionStorage.getItem("scroll_position"); // 300
sessionStorage.removeItem("scroll_position");
sessionStorage is really well-supported -- it'll will work fine for IE8+ any relevant version of the other browsers.
StackOverflow handles after-page-load scrolling by storing post id's in the URL hash. You could do that as well.
The url stackoverflow.com/...../21485393#21485393 has #21485393 which matches an anchor element <a name="21485393"></a> It will automatically scroll to that element after the page loads.
You could do something like that as well.
http://your.url.com/page#300
Retrieve it with
window.location.hash
And remove it once you're done by
window.location.hash = ""

Set value in textbox in external website when user clicks link on my website

I have my own website. There is a link on my website which redirect me to the externel website. I want to know how to set value in textbox in that externel website when user click link on my website
Have you tried to save opened window handler?
var openedWindow = window.open(....
Instead of using direct links.
Or maybe pass parameters through url?
you can add parameters on the URL.
just modify the link accordingly, and read the URL parameter on the other site.
(this assumes you don't care the possibility that someone fills the parameters with fake info)
Unless that site has implemented something to let you set that (e.g. if they populate the text field using data from the query string), you can't.
There is no standard mechanism for pre-populating forms via a link to the page containing the form.

Update URL parameter with javascript, without ruining the history?

I'm looking for a way to update the url in the status bar..
I have a gallery, and when you click your way through the gallery I want the image ID to show up in the URL, so that the user can link directly to the image..
I've read about using hash and so. but as far as I've tried it, that "ruins" the history.
If I click the back-button in my browser the previous image would be shown.
Is it possible to add or update a URL parameter, without ruining the history?
Thanks in advance
Use location.replace to replace the current location:
Replace the current document with the one at the provided URL. The difference from the assign() method is that after using replace() the current page will not be saved in session history, meaning the user won't be able to use the Back button to navigate to it.
Do it simply this way, when switching to images, add a hash to the url, for example:
location+='#image-'+image_id
your location will become
http://example.org/images/#image-3
instead of the initial
http://example.org/images/
and onload, check if location.hash is not empty, and matches with ^image-(\d+)$ (regular expression pattern), if it matches, do the usual thing you'd have done if a user clicks on image with id (\d+).
To preserve history, use reallysimplehistory.

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