Broken relative Url in jQuery getJSON Ajax Method - url

The Url for my development environment is:
http://localhost/mysite/blah...
I am using jQuery & getJSON to perform some ajax actions on my site, which work fine all the time I specify the url as:
/mysite/controller/action
..but this is not ideal as I don't want to hardcode my development url into my seperate jQuery include files.
When the site goes live, it'll be fine to have controller/action or /controller/action as the url as that will resolve ok, but for the development site, it's no go.
I've tried:
controller/action
..but this returns a 404, which suprised me as I thought the lack of / at the front of the url would prevent from looking at the website root.
There must be a neat solution to this?

I would do this by inserting a global constant in my HTML header:
<script type="text/javascript">
var BASE_URL = '/mysite/';
</script>
That would be inserted from your server so it can be dynamically changed.
Later in your script, you'll be able to make AJAX requests with (jQuery style here):
$.ajax( BASE_URL + '/controller/action', ...);

If you're in
/mysite/controller/action
then the correct relative path to
/mysite/some_other_controller/some_other_action
is
../../some_other_controller/some_other/action

You can use this code to get the current path of the .js script and use it for calculate your relative path.
var url;
$("script").each(function () {
if ($(this).attr("src").indexOf("[YOUR_SCRIPT_NAME.JS]") > 0) {
url = $(this).attr("src");
url = url.substr(0, url.lastIndexOf("/"));
return false;
}
});
var final_url = url + "/your_target_script.js"
Replace YOUR_SCRIPT_NAME with the unique name of your script.

Related

Passing a URL Parameter via a button link

Very simply, I'm looking for a way to grab the value of a URL parameter and pass this to a target URL inside a button.
Ex:
Page URL: https://example-domain.com/page-1?parameter=value1
In this page, there is a button with the link URL: https://example-domain.com/page-2
I'm looking for a way to have this link to be dynamically updated to https://example-domain.com/page-2?parameter=value1
Is there a way that I can handle without a deep understanding of any coding language for my Wordpress website?
Thanks in advance
The solution I found is:
LINK_TEXT_HERE
<script type="text/javascript">
jQuery(function() {
var el = document.getElementById('ELEMENT_ID_HERE');
el.href = el.href + window.location.search;
});
</script>

Yii URL Rules Rewrite

I have the folowing URL rule in main.php under urlManager, rules:
the-urly-bird => blog/content/index,
This page (http://www.accesstheflock.io/the-urly-bird) has 10 pages of content. I would like each page to have it's own URL like: http://www.accesstheflock.io/the-urly-bird/page/2.
The path for each page is: ?r=blog/content/index&Content_page=2
Please provide URL rules for main.php to make this work. This is very much appreciated. Thank you.
I was able to write working URL rules and add a jQuery on click event handler to redirect to the right url for the pagination.
main.php
'the-urly-bird/page/<page:\d+>'=>'blog/content/index',
'the-urly-bird' => 'blog/content/index',
view
$('li.page a').on('click', function() { //Redirects
window.location.href = $(this).attr('href');
return false;
}); –

Prepend a word to all ajax urls by some configuration change in rails app

I have a rails app in which I have used ajax calls with some url. I want to append project name to the ajax urls. One way is to edit all urls. Is there any other simpler way to do the same?
To prepend a word (let it be project_name) to all ajax calls in application write the following code in a js file:
$.ajaxSetup({
beforeSend: function(data, settings) {
var url = "/project_name"+ settings.url;
settings.url = url;
}
});

Titanium: How to redirect from external to internal URL in webview

So I know how to access both external and internal URL's in the Titanium Webview. But I have no idea how to redirect from an external url to the internal url.
I've got a file called "index.html" in the root folder, so for the webview this should work to access it:
Ti.UI.createWebView({
url: 'index.html'
});
External urls are pretty straight forward
Ti.UI.createWebView({
url: 'http://www.google.com'
});
However, when on the external url, how do I redirect to this local file? None of these work:
LOCAL?
LOCAL?
or the javascript variant
window.location = 'file:///index.html';
Any clues on how to do this?
What I discovered, in the end, are 2 possibilities to achieve this. But it can't be done through redirection.
One: Poll for a certain variable using the webview.evalJS() function
var my_data = $.login_webview.evalJS('global.data;');
Of course, it works only with strings, not with objects. So if you're passing JSON, make sure it is set as a string!
Two: Do an actual redirection server side, to another serverside page and monitor for URL change and then do evalJS() once again, but no need for polling
$.login_webview.addEventListener('load',function(e){
if (e.url.indexOf('mobile/redirect.php') > -1){
var my_data = $.login_webview.evalJS('global.data');
}
});
Just make sure, with 2 that you're actually setting the required data in Javascript using server side technology.

Hash-Key URL Parameters?

How does a url of this type get generated:
http://www.foo.bar/#/project/123
In Javascript:
location.hash = "/project/123";
And then when the application loads check for the hash, and redirect the user to the "hashed" url.
if(!!location.hash) {
location.href = "http://foo.bar" + location.hash.substring(1);
}
In .htaccess files you can set up Rewrites (if you're using Linux). On Windows machines, you would be using IIS to do the re-writes.
Refer to this page for more info

Resources