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.
Related
I have a web-app in NodeJS that make certain redirects in sometimes using the method meta refresh. The problem is that this method are incompatible with Firefox (in Firefox not work meta refresh). And I need that the redirect goes well in all browsers and versions. I understand that the best way is use window.location method, is right?
The structure of the code that make the actual redirect is:
module.exports = (outcome) => {
return "(function(document) {"+
"var meta = document.createElement('meta');"+
"meta.setAttribute('http-equiv', 'refresh');"+
"meta.setAttribute('content', '0;URL=" + outcome + "');"+
"document.head.appendChild(meta);"+
"})(document);";
}
How would the final code with the window.location method?
Thank you very much!
You can use window.location.href.
A simple example would be:
window.location.href = 'http://example.com';
Or wrapped in your function:
module.exports = (outcome) => {
return "(function(document) {"+
"window.location.href='" + outcome + "';"+
"})(document);";
}
This is the url which i am trying to hit from within .js file which contains knockout related function:
self.followAction = $.resolvePath("/People/Follow?uid=" + data.UserId);
here People is the controller and Follow is the action method, on button click, i want to send userId along so i have written this.
To resolve relative path from within javascript, i have written this function
// Fix for resolving relative paths from within js scripts
$.resolvePath = function(url)
{
var path = '#Request.ApplicationPath';
if (path != '/') return path + url;
return url;
};
But, on button click, i am getting this error: HTTP Error 404.0 - Not Found
and url is:
localhost:44305/People/#Request.ApplicationPath/People/Follow?uid=8
please tell me what should i try now. thnks in advance!
Razor code is not interpreted within JS files, hence the #Request.ApplicationPath is read as a literal string. You need to put that code somewhere where it will be executed so that your JS can read it; perhaps as a data-* attribute on an element in your View, something like this:
<!-- in a layout view... -->
<body data-app-path="#Request.ApplicationPath">
$.resolvePath = function(url) {
var path = $('body').data('app-path');
if (path != '/')
return path + url;
return url;
};
New Restful API's like Google, OpenStreetview use a simple call back mechanism.
Basically you call the API, adding a parameter &callback=my function.
When executing a call to this API, as a result my function is called passing a JSON dataset.
I am trying to create the same mechanisme for a API I am building for my personal use.
As far as I understood my API needs to return a javascript, that calls the function that is passed in a script.
For a test I created this:
function apiCall(URL,values, keyPair,cBackPair) {
// URL specifics URL to call
// keyPair: <keyname>=<key>; leave black if unneeded
// cBacPair: <callBackParametername>=<functionname>
// called is: URL?values&keypair&cBackPair
var request = (keyPair)?'&'+keyPair:'';
request = URL + '?'+ encodeURI(values) + request + '&' + cBackPair;
var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", request);
document.body.appendChild(script);
}
function callAPI() {
apiCall('http://xllent.nl/map/ajax/answer.php','q=one','','s=doit');
}
function doit(result) {
alert(result);
}
To test I call callAPI onload.
The script answer.php is very basic:
<?$s = $_GET['s'];
?>
<script type="text/javascript">
doit('jeroen');
</script>
Later the script would use $s to call the right script, and of course supply user data.
For now I am just trying to get the script doit('jeroen'); to be run. But nothing happens.
Typing javascript:doit('jeroen'); in the browser window gives the result I would expect.
Any suggestions?
Don't surround your javascript with <script> tags. You are not generating a HTML file with a javascript body.. You should think of this as if you're generating a javascript file on fly.
Javascript files also don't start and end with <script>
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.
I have an ASP.NET MVC3 application published to a url like this:
http://servername.com/Applications/ApplicationName/
In my code, I am using jquery ajax requests like this:
$.get(('a/b/c'), function (data) {}, "json");
When I run the application locally, the ajax request goes directly to the correct page (being an mvc route) because the local page ends with a "/" (localhost/a/b/c).
However, when I publish to http://servername.com/Applications/ApplicationName/, the trailing "/" is not always present. The url could be http://servername.com/Applications/ApplicationName, which then causes the ajax request to try to load http://servername.com/Applications/ApplicationNamea/b/c, which fails for obvious reasons.
I have already looked into rewriting the url to append a trailing slash, but A) It didn't work, and B) I feel like it's a poor solution to the problem, and that it would be better to configure the javascript urls to work properly regardless of the local folder setup.
I did try "../a/b/c" and "/a/b/c", but neither seemed to work.
Thanks in advance for the help!
Personally I tend to use a global variable of the relative URL of the server in my view like:
var BASE_URL = '#Url.Content("~/")';
Then you can do things like :
$.get(BASE_URL + 'a/b/c'), function (data) {}, "json");
I would like to add that if you want it to be totally global, you could add it to your /Views/Shared/_Layout.cshtml instead.
I ran into the same problem, and ended up creating two JavaScript functions that mirror the functionality of the MVC Url helper methods Url.Action and Url.Content. The functions are defined in the _Layout.cshtml file, so are available on all views, and work regardless of whether the application is in the root of the localhost or in a subfolder of a server.
<script type="text/javascript">
function UrlAction(action, controller) {
var url = ('#Url.Action("--Action--","--Controller--")').replace("--Action--", action).replace("--Controller--", controller);
return url;
}
function UrlContent(url) {
var path = "#Url.Content("~/--file--")";
path = path.replace("--file--", url.replace('~/', ''));
return path;
}
</script>
These can then be called like so:
var url = UrlAction('AvailableAssetClasses', 'Assessment');
var url2 = UrlContent('~/Images/calendar.gif');
Always use Url helpers when generating urls in an ASP.NET MVC application and never hardcode them. So if this script is directly inside the view:
<script type="text/javascript">
var url = '#Url.Action("a", "b")';
$.get(url, function (data) {}, "json");
</script>
And if this script is inside a separate javascript file (as it should be) where you don't have access to server side helpers, you could simply put the url in some related DOM element. For example using HTML5 data-* attributes:
<div data-url="#Url.Action("a", "b")" id="foo">Click me</div>
and then in your javascript file:
$('#foo').click(function() {
var url = $(this).data('url');
$.get(url, function (data) {}, "json");
});
and if you are unobtrusively AJAXifying an anchor or a form, well, you already have the url:
$('a#someAnchor').click(function() {
var url = this.href;
$.get(url, function (data) {}, "json");
return false;
});