window.location.assign(varString) wont work - javascript

I have java script function to check the URL and split it,
I ask a question and depends on the answer it will forward the user page
all works fine until I use window.location.assign();
with string inside (=window.location.assign(path);) instead of fixed URL (=window.location.assign("http://stackoverflow.com");)
what can i do?
thanks...
var register=...;
var login=...;
function link(type) {
var urlPath = document.URL.split("/");
if (type == "register") {
var path= urlPath[2] + register;
window.location.assign(path);
}
else {
var path = urlPath[2] + login;
window.location.assign(path);
}
event.preventDefault();
}

You should use the full URL.
window.location.assign(urlPath[0]+'/'+urlPath[1]+'/'+urlPath[2]+register);
window.location.assign(urlPath[0]+'/'+urlPath[1]+'/'+urlPath[2]+path);
Or
window.location.assign(window.location.origin+register);
window.location.assign(window.location.origin+path);

Related

Displaying Docs w/n a View Panel via a Dialog

I have an XPages app, whereas I have an XPage that contains a viewpanel and two Custom Controls...both custom controls are dialogs that are used to entered information. All this works perfectly. However, what I am interested in is: how do I get a handle on selected/clicked document and display it via a dialog. I am somewhat familiar for with the "var" variable w/n the viewpanel properties, but am not sure this is the right approach, or even how to finish it. Can someone advise as to how to accomplish this, or even if I should go about it differently? Thanks in advance.
onClick - vwColumn & pageURL event:
var dataRes;
if (rowData.isCategory()) {
return "";
}
var href = facesContext.getExternalContext().getRequest().getContextPath();
try {
var doc = rowData.getDocument();
if (doc != null) {
var docID = doc.getUniversalID();
var formType = rowData.getColumnValue("Form")
if(formType == "Memo") {
dataRes = href + "/memoXP.xsp?documentId=" + docID + "&action=openDocument";
} else {
dataRes = href + "/";
}
}
} catch (e) {
#WarningMessage(e)
}
if (doc != null) {
doc.recyle();
}
return dataRes;

I want access address bar content in JavaScript

I'm trying to route to a dynamic page in JavaScript,
Is there any way I can do this,
localhost/page.html/001
Can I write a code like,
If (url last characters == 001) {
//Do something
}
<script type="text/javascript">
var lastUrl = window.location;
lastUrl = lastUrl.replace("localhost/page.html/", "");
if(lastUrl == "001"){
alert(lastUrl);
}
</script>
You could use:
const currentUrl = window.location.href
To get the current URL of the side (In this case localhost/page.html/001), then:
const filterUrl = currentURL.split("/");
if (filterUrl[2] === '001') { /* Do stuff */ } else { /* Do other stuff */ }
If your URL's are going to be always like that you could use this little snippet to filter them.

Javascript/html keep url params

say I provide someone with a link to my page that looks like the following
www.linkone.com?something=ten
When they visit the page, the param will be visible within the url for that page. If on that page I then have a link like so
Link Two
When they click on it, Link Two's url will look like
www.linkone.com/linktwo.html
Is there any way to add the param from the first link onto any other links within the page? So when they visit Link Two, I want to url to end up being
www.linkone.com/linktwo.html?something=ten
I know it is probably possible via javascript, but the homepage has a lot of links on it to other pages within the site, and I need to param on all links. What is the best way to achieve this?
Thanks
$('a:not([href=#])').on('click', function (e) {
e.preventDefault();
location.href = this.href + "your param";
});
My suggestion is:
$('a:not([href=#])').click(function(e) {
e.preventDefault();
// Getting query-string parameters from current page URL.
var query = location.search.substr(1);
// Getting URL from current link;
var url = new URL($(this).attr('href'));
// Updating query-string parameters of current link.
url.search += (url.search.indexOf('?') == 0 ? '&' : '?') + query;
// Getting the link target.
var target = $(this).attr('target');
// Navigating to new URL.
if (target) {
window.open(url.toString(), target);
} else {
location.assign(url.toString());
}
});
One way to achieve a similar function is to use cookies or session. When a user visits your site with additional param, set a cookie on the parent page with that param so that it would be available for other calls/links. You could do that in session as well. session would retain the value till the session times out and cookie expires based on the expiry date set for it.
Link to Cookie Reference
You can update anchors href attributes at Document Ready. Below code solution for current HTML querystrings carries over the selected anchors href attributes.
var selectedAnchors = $("a");
selectedAnchors.each( function () {
var itemHref = $(this).attr("href");
if ( itemHref.indexOf("?") > -1 )
itemHref += "&";
else
itemHref += "?";
itemHref += location.search.substr(1);
$(this).attr("href", itemHref );
});
Please try this. Just replace word "something" with your own parameter.
Thanks
$("a").click(function(e){
e.preventDefault();
var a = $(this).attr('href');
var something = getUrlParameter('something');
var newURL = a+"?something="+something;
window.location.href = newURL;
});
var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
};
HTML:
Google
This is possible (with php):
<a href="http://www.linkone.com/linktwo.html?something=
<?php echo $_GET["something"]; ?>"> Link Two </a>

Difficulty Loading New Page w/ Javascript

I'm writing a basic Flask app. I have a page called /searchByCollege that consists of a bunch of buttons, each with a team name as their text, and .college as their class.
I've written some JS so that, when the user clicks on a button, it'll load /searchByCollege/collegeName, where collegeName is the text of the button they just clicked on. Here's what I have:
<script>
$('.college').on('click', function() {
var baseURL = $('#baseURL').text();
var finalURL = baseURL + "/" + this.text();
window.location.href = finalURL;
return false;
})
</script>
I didn't originally include return false; and nothing happened upon clicking a button. Then I added return false; and I got the same result. I've inspected the HTML and the base URL is correct (it's just /searchByCollege). I've looked at the requests as I click on the button and none are being made.
I've loaded jQuery above this through Google's CDN so that's not the issue.
Any other ideas?
Thanks for the help,
bclayman
this.text()
needs to be changed to
$(this).text()
You need to wait for the document to load by using $(document).ready:
<script>
$(document).ready(function(){
$('.college').on('click', function() {
var baseURL = $('#baseURL').text();
var finalURL = baseURL + "/" + this.text();
window.location.href = finalURL;
return false;
});
});
</script>
I could not figure out where you were getting the 'baseURL' Since you are using just JQuery you need to call .value and not .text()
<button class="college" value="CSU">CSU</button>
$('.college').on('click', function(){
var baseURL = "/searchByCollege";
var finalURL = baseURL + "/" + this.value;
return false;
});
you can try this
window.location.hostname = finalURL;
window.location.pathname = '';

JS: incrementing pages (page1.html, page2.html,page3.html...) for window.replace

What approach should for this scenario. I want the page to go to the next page after it finishes all the function it needs to.
so, example. after all function for page1.html has been done, it will call a function next_page().
next_page() function will evaluate the current page and add "1" it. so from page2.html it will now be page3.html. page3.html will also contain the same function of the previous html, that after all the functions have been done, it will call the next_page() function that will also evaluate the current and increment it.
//current_url = "mysite.com/page1.html"
var current_url = window.location;
var end_page = "mysite.com/page12.html"
var increment_url = eval(current_ur + 1 );
if(current_url != end_page ) {
setTimeout(next_page,2000)
}
else {
alert("this is the last page!")
}
function next_page() {
window.location.replace(increment_url);
}
var increment_url = addone('mysite.com/page1.html');
returns mysite.com/page2.htm
function addone(url) {
var pattern=new RegExp("(.*)([0-9+])(\..*)");
var match=pattern.exec(url);
return match[1] + (parseInt(match[2]) + 1 ) + match[3];
}
​
so assuming the example URL you gave is accurate enough that the regular expression will work use
var increment_url = addone(current_url);
The easiest thing to do would be to have a hidden input on each page that has the value of the next page url. This would allow you to use arbitrary page urls and still be able to get the effect you want.
Using jQuery
$(function() {
setTimeout(function() {
var url = $('#next_page_url').val();
window.location.replace(url);
}, 2000);
});
<input type="hidden" id="next_page_url" value="http://mysite.com/page2.html" />

Categories