I have a button in my external js file that, when clicked, I want to be sent to a controller that I have. When I have the on click function in JQuery with the url set to my localhost, it works fine, but how do I set it so that it would work similar to the way the base_url works in CodeIgniter, so that it would work in any environment? I've tried using window.location.origin, or window.location.host instead of the localhost, but neither work. Any ideas?
My external JS button:
var html = '<div class="alert alert-success" role="alert"><p class="instruction_text text-center">' + completedText + '</p></div><input id="next_task" class="btn btn-lg btn-default text-center" style="margin-top: 150px; font-weight: bold;" type="button" value="' + text[lang]['continue'] + '"data-url="http://localhost:8888/oda/sample/instructions/' + task_id + '/' + lang + '/' + type + '">';
$('#final_message').append(html);
$(document).on('click', '#next_task', function(e) {
e.preventDefault();
location.href = $(this).data('url');
});
Thanks!
You can leave that work to the browser. If the page is within the same domain you don't need to put it verbosely on your code, you can put something like that:
'"data-url="/oda/sample/instructions/' + task_id + '/' + lang + '/' + type + '"'
note that the / on the beginning says that it will always get to the same place, disregard the current url.
But, if you need for some reason to write the full url window.location.origin will not include the port part of your url, you would have to include window.location.port too to make it work with your environment. and it's not a necessary thing to do in production.
window.location.host should work if you put http:// and / on your string:
'"data-url="http://' + window.location.host + '/oda/sample/instructions/' + task_id + '/' + lang + '/' + type + '"'
You can define base_url value to the JS variable in view and include all external JS below that
Here, I am giving section of example head.php which is located at /your_root/application/views
<script>
var base_url = "<?php echo base_url(); ?>";
</script>
Related
I have problem with IIS does not read the URL.
Failed to load resource: the server responded with a status of 503 (Service Unavailable)
My project is on Asp.net MVC
I have used a Jqx grid on my project but the grid does not load data on IIS.
How can I convert the URL to Action Link #url.action, so the IIS can read?
My URL on my grid.
url: "/Home/GetIMScompinfo?CompanyName=" + $("#CompanyName").val() + "&StoreName=" + $("#StoreName").val() + "&ComputerName=" + $("#ComputerName").val() + params
url: "/Home/GetSubItems?scopeId=" + id
url: "/Home/GetIMScompinfo?CompanyName=" + $("#CompanyName").val() + "&StoreName=" + $("#StoreName").val() + "&ComputerName=" + $("#ComputerName").val() + params
url: "/Home/GetSubItems?scopeId=" + id
I don't know what is the difference between the two getting the value. So I give the sample script for the first url.
<button class="btn-class" id="press" type="button" onclick="changeHref1()">Genrate first url</button>
<script type="text/javascript">
function changeHref1() {
var url = '#Url.Action("About","Home")';
var company = $('#CompanyName').val();
var store = $('#StoreName').val();
var computer = $('#ComputerName').val();
window.location.href = url + '?CompanyName=' + company + '&StoreName=' + store + '&ComputerName=' + computer;
}
</script>
Heyho, I want to add GET params like?USER_NAME=Max&USER_ID=01 to the URL if the page gets reloaded. I already got a few snippets for changing the URL / adding params. But I need something to detect the page reload and execute a function. Or a way to change the path of the Url directly if the page gets reloaded.
I tried several things like beforeunload and navigation types.
$(window).bind('beforeunload', function() {
// EDIT: vars like uname etc are defined
var newurlgetstring = "?USER_NAME=" + uname + "&USER_EMAIL=" + uemail + "&LAST_NAME=" + lname + "&PRE_NAME=" + fname + "&UTITEL=" + utitel + "&U_ID_T=" + uidt + "&MV=" + mv + "&V=" + uv ;
var newurl = window.location.protocol + "//" + window.location.host +window.location.pathname + newurlgetstring;
window.history.pushState({path:newurl},'',newurl);
});
But I want able to execute a function with beforeunload. I was just able to display a return question.
I need to replace one link from javascript with php link.
javascript line:
'<a href="' + res.attachment.guid + '" target="_blank">
php :
echo '<a href="' . wp_get_attachment_url($value->ID) . '" target="_blank">
javascript context:
if (res.success) {
$('.no_file_upload').remove();
var template = '<li class="attachment-' + res.attachment.ID + '">' +
'<p>' + res.attachment.post_title + '<span>' +
'<i class="fa fa-cloud-download" aria-hidden="true"></i>' +
'<i class="fa fa-times" aria-hidden="true" data-post-id="' + res.attachment.ID + '" data-project-id="' + res.attachment.project_id + '" data-file-name="' + res.attachment.post_title + '"></i>' +
'</p></span>' +
'<span>' + res.attachment.post_date + '</span>' +
'</li>';
I tried this way but fail:
<?php echo '<a href="' . wp_get_attachment_url($value->ID) . '" target="_blank"> ?>
So my Wordpress site is uploading all the files to Amazon S3 using WP Offload S3 Lite plugin. I had to alter some of the template files to manage to show the proper Amazon S3 link on website, I had change href links with wp_get_attachment_url($value->ID). The only thing left is one javascript file with that content that is still loading wrong download link. I need to make it work.
I think better you use file name instead of attachment url. get_attached_file( $data->ID )
https://developer.wordpress.org/reference/functions/get_attached_file/
also you can use https://wordpress.org/plugins/amazon-s3-and-cloudfront/ for correct url
for 1.
when you have file name you can generate your own link with a custom function.
Please let me know if there is any issue.
javascript line
<a href="' + cloudfront_url + res.attachment.guid + '" target="_blank">
Complete Javascript
var cloudfront_url = 'https://your_unique_id.cloudfront.net/'
if (res.success) {
$('.no_file_upload').remove();
var template = '<li class="attachment-' + res.attachment.ID + '">' +
'<p>' + res.attachment.post_title + '<span>' +
'<i class="fa fa-cloud-download" aria-hidden="true"></i>' +
'<i class="fa fa-times" aria-hidden="true" data-post-id="' + res.attachment.ID + '" data-project-id="' + res.attachment.project_id + '" data-file-name="' + res.attachment.post_title + '"></i>' +
'</p></span>' +
'<span>' + res.attachment.post_date + '</span>' +
'</li>';
}
You're trying to mix Javascript & PHP code. That is possible but let me explain how it works. PHP code is executed on server side & Javascript is executed on client side.
So, if you write down any php code in the javacsript code, it'll be executed at the time the HTML page is sent by the server to the user. Once the page is loaded, you can't call any PHP function inside the javascript code.
The PHP code is executed in the server before executing the JavaScript client side, the only way in your case is by using AJAX call to get the download link from the server.
In the other hand, there is an option to write JavaScript inside PHP, but not the inverse.
I'm trying to generate a link using following
$(this).parent().attr('href', '#Url.Action("Create", "Home"' + '?clientId=' + clientId + '&clientName=' + clientName);
somewhere I read that I need to isolate this Url.Action with controller and action into variable so I tried with this
var a = '#Url.Action("Create", "Home"';
$(this).parent().attr('href', a + '?clientId=' + clientId + '&clientName=' + clientName);
But this still doesn't work.
In browser I'm getting
http://localhost:1328/Home/Index2/#Url.Action%28%22Create%22,%20%22Home%22?clientId=181&clientName=undefined
Another option is to store the url with data-* attributes and access them. In the View you can add the below attribute:
data-url='#Url.Action("Create", "Home")'
Now you can access this in the script with:
var base = $(this).data('url');
$(this).parent().attr('href', base + '?clientId='+ clientId +'&clientName=' + clientName);
Your script should be on Razor page in order to make #Url.Action helper work.
When you place it there this should work:
//this line should generate /Home/Create string
var urlNoParam = '#Url.Action("Create", "Home")';
//and here you just add params as you want
$(this).parent().attr('href', urlNoParam + '?clientId=' + clientId + '&clientName=' + clientName);
I am trying to share my custom link on facebook using this javascript function,
function postToFacebook(title, summary, image_url, U)
{
window.open('http://www.facebook.com/dialog/feed?app_id= ' + fb_app_id + '&display=popup&caption=' + title + '&description='+ summary + '&redirect_uri=' + encodeURIComponent('http://facebook.com') + '&link=' + encodeURI(U) + '&picture=' + image_url,'sharer','toolbar=0,status=0,width=548,height=325');
}
In my "U" parameter I have this link => "http://localhost:4000/rings/solitaire-rings/lst?utf8=%E2%9C%93&min_price=&max_price=&name=solitaire-rings&category=rings&fashion_type=false&search=&gallery_order=&new_arrival=&best_seller=&metal_white=on&commit=APPLY"
and when user clicks on the link which is shared on facebook , user should get redirected to this link. forget about root url i.e. localhost:4000, because on production environment its considering required url.
the problem is that whne i am sharing this url on facebook user is getting redirected to "http://localhost:4000/rings/solitaire-rings/lst?utf8", and its not taking care of further parameters which are "%E2%9C%93&min_price=&max_price=&name=solitaire-rings&category=rings&fashion_type=false&search=&gallery_order=&new_arrival=&best_seller=&metal_white=on&commit=APPLY"
basicaaly I supposed to get this URL "http://admin.velvetcase.com/rings/lst?utf8=%E2%9C%93&min_price=&max_price=&name=rings&fashion_type=false&search=&gallery_order=&new_arrival=&best_seller=&search_material_id[]=Diamond&commit=APPLY" , but when I am sharing mu URL on facebook I am getting this one "http://admin.velvetcase.com/rings/lst?utf8=%E2%9C%93&%3Bmin_price&%3Bmax_price&%3Bname=rings&%3Bfashion_type=false&%3Bsearch&%3Bgallery_order&%3Bnew_arrival&%3Bbest_seller&%3Bsearch_material_id[0]=Diamond&%3Bcommit=APPLY" , which is not giving e required output.
Here is what I did successfully
var sharingUrl = "https://www.facebook.com/dialog/feed?app_id="+ facebookAppID +
"&link=" + sharingUrl +
"&name=" + encodeURIComponent(title) +
"&caption=" + encodeURIComponent(caption) +
"&description=" + encodeURIComponent(content) +
"&picture=" + encodeURIComponent(thumb) +
"&redirect_uri=https://www.facebook.com";
window.open(sharingUrl);
For all parameters we need to use encodeURIComponent, but I try to not use encodeURIComponent for sharingUrl, it still works!