Call Controller Action through Javascript - javascript

I have call controller action through javascript using window.location
window.location = "/SomeController/SomeAction/";
it's working fine but when i will develop it in on sub-domain it not constructing URL properly
My URL Is
http://testgecianet/pms/
when i call the action it construct URL like
http://testgecianet/SomeController/SomeAction
instead of
http://testgecianet/pms/SomeController/SomeAction
how i can construct correct path when application deploy on SubDomain.?

did you try using #Url.Action ?
For example your code of
window.location = "/SomeController/SomeAction/";
could be written like
window.location = "#Url.Action("SomeAction","SomeController")";
This could solve the problem, I hope.

Related

Passing javascript to Rails helper

I created a simple javascript on getting the URL on my browser under my application.js file:
function get_url {
var url = window.location.href;
return url;
}
Then on my helper/application_helper.rb file I called the javascript:
def get_full_url_helper
javascript_tag(
"get_url()\";"
)
end
I tried to pass this on other helper just to see if its getting the full url:
def active_url
get_full_url_helper
byebug
end
Upon using byebug instead of getting the full url on the browser it returns this weird <script>//<![CDATA[] get_url </script> thing. For some reason its not calling the right function so I can get the text URL and so something with it.
Any idea what am I missing here?
Shouldn't your function get_url be:
function get_url() { // <--- parentheses were missing in your sample
var url = window.location.href;
return url;
}
Also, a simple way to debug what the issue might be (and perhaps find out where your issue might be) is to just modify your get_full_url_helper to be:
def get_full_url_helper
javascript_tag(
"console.log('hello!');"
)
end
and then verify whether it is being invoked on your page in developer console tab in your browser.

Remove parameter from url not working

I'm attempting to remove a url parameter status from the url but in the following alert, the parameter is still there.
var addressurl = location.href.replace(separator + "status=([^&]$|[^&]*)/i", "");
alert(addressurl);
location.href= addressurl;
How do i solve?
You are confusing regex with strings.
It should be:
var addressurl = location.href.replace(separator, '').replace(/status=([^&]$|[^&]*)/i", "");
Javascript context in web pages are to the page you are working on.
When you reload, redirect or move to any other page, javascript changes done in previous page will not be there. This has to be handled from server side.
Refresh repeats the last request to the server, which is going to ignore your javascript changes. Instead navigate to the new url with window.location = addressurl;

PLAY: How to create hyperlinks in a javascript file that link to a controller method?

I am building a WebApp with Play Framework 2.2.1 and want to generate hyperlinks in my javascript file that links to a controller method. I have already created a jsRoutes object to make ajax requests:
jsRoutes.controllers.Application.getData(id).ajax({...});
But what I now want is to save the url into a variable:
var url = jsRoutes.controllers.Application.test(par1, par2).url();
.url() doesn't work and I don't know what functions this object provides. Is there some way?
For me it works without parenthesis at the end:
var url = jsRoutes.controllers.Application.test(par1, par2).url;
This way you get relative URL (path). You can also get HTTP method like this:
var method = jsRoutes.controllers.Application.test(par1, par2).method;
By the way, you can find out what functionality does an object support by using "console.log" function in Google Chrome:
console.log(jsRoutes.controllers.Application.test(par1, par2));
There I was able to see that "url" is not a function and therefore parenthesis are not allowed.

Making A Dynamic HTML Page to Redirect to A URL Using JavaScript

I need to make A Dynamic HTML Page to Redirect to A URL Using JavaScript.As a beginner, i need your help...What i want to do is to redirect to a url through a html page.For example: suppose a page address is www.example.com/pages.html?http://yahoo.com/news so this pages.html page in this case will redirect a user to yahoo.com/news ....I know how to do this with PHP but i cant understand how i should do it with javascript and HTML . any idea? Thanks
This should do it:
function Redirect(){
var current = window.location.href;
var exclude = current.indexOf('?');
window.location = current.substr(exclude + 1);
}
setTimeout("Redirect()",5000);
You can use window.location.href="new-url.html"; to go to a URL with JavaScript.
To parse the URL, use var theURL=window.location.href.split("?")[1];
location.href = location.href.split("?")[1];

Forcing Javascript Redirection

I am trying to implement what seems to be very simple JavaScript redirection, via the following rudimentary command:
window.location.href = "http://www.somesite.com";
So far so good, it works. I also can do it via the following method:
location.replace("http://www.somesite.com");
No problem here, it works again! The problem comes when I loose the protocol out of the string:
window.location.href = "www.somesite.com";
OR:
location.replace("www.somesite.com");
It just appends the new location to the current url:
www.currentsite.com/www.somesite.com
Of cause, that's not what I want. Is there any way to force the redirect?
One way is to use protocol-relative url like this:
window.location = "//www.somesite.com";
Or
window.location = "//somesite.com";
This way, it would redirect and browser itself will take care of figuring out protocol part eg http or https
Working Example
The protocol is required.
How else would the browser know whether
location.replace("mysite.pl");
was going to a Polish website or a Perl script on the current website?
You could do something like this to add http:// to the URL if it's not already there... although I can't think of a reason for not just including it yourself. Why complicate things?
​function redirect(url) {
if(url.substr(4) != "http")
url = "http://" + url;
window.location.href = url;
}
redirect("www.google.com")
​

Categories