How do I change a domain address variable with jQuery? - javascript

I need to be able to change a domain variable (or set it if it's not already set) and then reload the page. I do not care if the page doesn't reload in fact I need it to reload.
For example if a user is on page: sample.com/demo?theme=theme1
and they select a new theme from the dropdown I want to change the theme variable to navigate the browser to the same page with the new domain variable, so in this case sample.com/demo?theme=newtheme.
I can't just do a standard redirect because I don't want them just going to the homepage I want them to stay on the current page just have the variable changed. For instance if they were here: sample.com/demo/aboutus?theme=theme1 then they would be sent here: sample.com/demo/aboutus?theme=newtheme

A simple regex would do:
window.location = window.location.url.replace(/(\?|&)theme=[^&]+/, '$1theme=newtheme');

Just make your anchor tag relative:
Theme 1
Theme 2
Theme 3
the only downside is the current querystring will be completely replaced with the new one, if that is even an issue.
This can of course be done with javascript too:
window.location = "?theme=theme1";
//window.location = "?theme=theme2";
//window.location = "?theme=theme3";

Related

How to create a div on a page when clicking a button on another page

I need to write new text in a p tag on the 'cart' page when I click on the 'submit' button on the 'index' page. How can I do this with JavaScript. I have already tried with this
HTML page index :
<button onclick="myFunction()"> click me </button>
HTML page cart :
<p id="id"> </p>
Js code :
function myFunction() {
document.getElementById('id').innerHtml = 'mytext';
}
but it does not work. How I can solve this? Thank you
index page and cart page in this reference, are different documents.
Accordingly, when you call your function on the index page, it can't find an element with the mentioned id in that document.
function myFunction() {
document.getElementById('id').innerHtml = 'mytext';
}
The only way to perform this trick with only Javascript, is to use Cookies.
You can create a Cookie in the client's browser and save 'mytext' as an object, then retrieve it on another page.
See this page https://www.w3schools.com/js/js_cookies.asp
If you're not using a SPA structure with a framework like React or Vue this could be difficult - however its not impossible.
What you can do to circumvent the issue is that you can save a variable in sessionStorage or localStorage depending on whether or not you want it to remain between different browser sessions, then in your other page check if this variable exists and if it does make the required changes.
Then when it should change back you remove the variable from your localStorage again.
It's a bit of a hacky way, and the best way would be to either do this server side or to have a SPA and share the variable through global state.

What would be the simplest way to automatically have the URL segment updated?

This question is about the URL Update feature when changing the value of a Page Title field. The behaviour is coded into CMSMain.EditForm.js.
I'm stripping down and customising the CMS to be usable by an absolute basic computer user or the negligent client, who will most likely skip pressing the Update URL button upon page name change. In these cases it would be very convenient if the URLSegment would be automatically updated.
Q: What would be the simplest way to automatically have the URL segment updated, IE simulate the result that would appear upon clicking the "Update URL" button, after changing the Title field?
You could make an extension for SiteTree and include the function onBeforeWrite like this. This will make a change if they updated the Title and not the URL:
class AutoURLSync extends Extension {
public function onBeforeWrite() {
// If Title is changed, but URLSegment is not,
// then update the URLSegment here
if($this->owner->isChanged('Title',2) && !$this->owner->isChanged('URLSegment',2)) {
$this->owner->URLSegment = $this->owner->generateURLSegment($this->owner->Title);
}
}
}
Removing the "if" would mean it always is changed.
Add this in _config/config.yml to link up the extension:
SiteTree:
extensions:
- AutoURLSync

How do I grab the current page URL and place it into a new URL reference in JavaScript?

Here's what I'm using as a custom button control in Salesforce that executes JavaScript.
location.replace('/email/author/emailauthor.jsp?retURL=/{!Case.Id}&p3_lkid={!Case.Id}&rtype=003&p7=GetURLHere&p2_lkid={!Case.ContactId}&template_id=00Xc0000000HrOk');
The GetURLHere part is where I'd like to grab the current URL and place it on the next page.
This form works like Select Listview > Click Button > New page loads with the previous pages URL in the p7 field.
p7represents a field I'd like to put it into upon page load.
The usual document.location.hrefdoesn't want to work in this control.
Any idea on how to do this in the Force.com environment?
You should be able to use the $CurrentPage object to get the current pages url as a formula expression.
document.location.href = '{!$CurrentPage.URL}';

How to avoid Master Page hidden field value lost on new page load.

I have a hidden field on master page. I am setting its value on a child page through JavaScript. When I click link on this child page it redirects to another page. Now I want to get the hidden field value on this new child page. But the value is lost(obviously). How can I persist this value.
Anyone help thanks in advance.
You can pass the value in the query string like this:
http://YourUrl.com/YourPage.aspx?YourValue=foo
Alternatively, you can initiate an AJAX call that stores the value in the session and then retrieve the value on the page_load event of the next page.
You can add the value to the link when ever you change it.
Or you can use ajax to send the new value to the server after the change.
Your basic choices are to use javascript to either append it the link url as a querystring value or store it in a cookie.
visit these links might be it helps you
http://www.codeproject.com/Answers/219800/How-to-get-hidden-field-value-using-javascript-in-.aspx#answer3
How To: add dynamically HiddenField in masterpage base page
You mentioned that the page is redirect to another page, that means you must be using Response.Redirect or client side redirect. In this scenario you would not be to persist the hidden field value. To achieve this you might use a ajax call to save the value in a session variable and reload it on a appropriate master page event.
you can do that by handling the OnPageLoad event of your Master-page and assigning the value there too.
Kind of defeats the purpose of master pages eh? That was what the original intention was.
use viewstate in the masterpage it works perfectly for this type of scenario
public string NavigateUrl
{
get
{
string text = (string) ViewState["NavigateUrl"];
if (text != null)
return text;
else
return string.Empty;
}
set
{
ViewState["NavigateUrl"] = value;
}
}

Insert javascript variable into a form on another page?

I am wondering if there's a good way to take a variable built in Javascript and then insert it into a form that's on another page.
My application is a survey: I've got it so that at the end of the survey, all the results are displayed so they can look over their answers. Now I want to have the user click a link and have the answers of the survey show up automatically in the body of the form where they'll then add their email and contact info and click "send."
Any ideas?
Knowing that this ISN'T possible is fine too...if not, what alternate methods might I accomplish the end result?
You can create a hidden field in first page and change that value from javascript variable. When you post that page it can be another page where you need to display in form element.
EDIT
If you need without form in first page you need to link with hyperlink like below.
<a href='#' onClick='OpenPage()'> Go To Result </a>
<script>
var val=20 ; // say this is the value you want to pass to second page
function OpenPage ()
{
window.location.href = "resultpage.php?param=" + val;
}
</script>
After you need to read that param in resultpage either with javascript through URL string or from server side (in PHP $_GET).
Hope it helps.

Categories