I am trying to call a laravel route through javascript function. I have explored all the way on internet and over here but I am not finding up my solution. I referred to one answer over stack overflow about how to call route in javascript function but it didn't work for me. My route:
Route::get("edit_contact/{id}",'ContactController#edit');
Javascript function:
{
var url = '{{ route("edit_contact",":id") }}';
url = url.replace(':id', count);
document.location.href = url;
}
count is defined up in javascript which I need to pass in route.
Issue is this that it is continuously giving error..
Route [edit_contact] not defined.
All these routes are working fine when hitting through html. I have also tried to call up other routes as well through this, but none is working.
Name your route like this:
Route::get("edit_contact/{id}",'ContactController#edit')->name('edit_contact');
var routeUrl = route('posts.index');
var routeUrl = route('posts.show', {post: 1337});
// Returns results from /posts
return axios.get(route('posts.index'))
.then((response) => {
return response.data;
});
Try
let count=3
document.location.href = `edit_contact/${count}`;
try this piece. it should work:
Route::get("edit_contact/{id}",'ContactController#edit');
javascript code
<script type="text/javascript">
$("#valueid").change(function(e){
var id= e.target.value; // the id to be pass
//alert(id);
//checking the state of the domain if secured or not
if ("https:" == document.location.protocol) {
//alert('secured');
location.href="https://www.example.com/edit_contact/"+id;
} else {
//alert('not secured');
location.href="http://www.example.com/edit_contact/"+id;
}
});
</script>
In route file: (web.php)
Route::get("edit_contact/{id}",'ContactController#edit');
In js code:
Here I used count value 3 just for an example. You can use your actual value of count.
<script>
var count = 3;
var baseurl = window.location.protocol + "//" + window.location.host;
var url = baseurl + '/edit_contact/' + count;
document.location.href = url;
</script>
Related
I want to navigate to a URL on my site. This is what I have:
var TheDemoURL = window.location.host;
if (SomeCondition1) { TheDemoURL = TheDemoURL + '/fr/demo'; }
if (SomeCondition2) { TheDemoURL = TheDemoURL + '/de/demo'; }
...
window.location.replace(TheDemoURL);
Initially, in the variable watch, I have TheDemoURL: "localhost:49173" and when I alert the final TheDemoURL is looks a good URL but in reality nothing happens.
Why is this not working?
Ok, for those who come here, the solution was to add this:
var TheDemoURL = window.location.protocol + '//' + window.location.host;
Not sure if this is specific to asp.net but it made it work.
Try using
self.location = TheDemoURL;
This will take into account iframes and other weirdness.
I called this method on keyUp event of enter key
function PerformSearch(search_key) {
var url = "<%=ResolveUrl("/Test/TestPage.aspx?type=search&search_key=")%>" + search_key;
//GoToLocation(url);
//window.open(url);
//$(location).attr("href", url);
//window.location.replace(url);
window.location.href = url;
}
use like this
window.location = url;
For your reference read this
You have string concatenation errors in your url.
This:
var url = "<%=ResolveUrl("/Test/TestPage.aspx?type=search&search_key=")%>" + search_key;
Should be this:
var url = "<%=ResolveUrl('/Test/TestPage.aspx?type=search&search_key=')%>" + search_key;
I have 2 URL like this:
http://localhost/gtwhero/public/users/3/subscribers-lists/1/subscribers
http://localhost/gtwhero/public/users/3/subscribers-lists/
So, URL format is like it:
{baseURL} +"users/{user_id}/subscribers-lists/"+{id}+"/subscribers"
Or-
{baseURL} +"users/{user_id}/subscribers-lists/"
and when I run this JS in this 2 pages, because 1page code is included in other page:
var baseURL = "http://localhost/gtwhero/public/";
$('#subscribers_list tbody').on( 'click', 'button.details', function () //Handeling Edit Button Click
{
var data = dataTable.row( $(this).parents('tr') ).data();
//alert(data['id']); //id = index of ID sent from server
window.location = window.location.href +'/'+data['id']+'/subscribers';
});
So, it is working for the second URL, but bot working for the first URL.
So, I am getting:
{baseURL} +"users/{user_id}/subscribers-lists/"+{id}+"/subscribers"
for the second URL, but getting error like this:
{baseURL} +"users/{user_id}/subscribers-lists/"+{id}+"/subscribers"+{id}+"/subscribers"
for the first URL.
Is there any solution?
I have done it-
var url = window.location.href;
var userId = (url.match(/\/users\/(\d+)\//) || [])[1];
var subscribers_lists_Id = (url.match(/\/subscribers-lists\/(\d+)\//) || [])[1];
i have soure code :
$("#yes-modal").click(function (e) {
var img = $("#url").val();
window.location = '#Url.Action("DownloadImages", "Hrd", new { id = *value variable img* })';
});
i want get value img, but i have error.
How can do it ? thank you.
Below syntax should work.
window.location = '#Url.Action("DownloadImages", "Hrd", new { id = ' + img + '})';
Let me know if you face any issue.
The razor syntax is a serverside code. It is evaluated while rendering the page from server. But the javascript is evaluated at the client side (browser). So you have to split it like below code.
$("#yes-modal").click(function (e) {
var img = $("#url").val();
var loc = '#Url.Action("DownloadImages", "Hrd")';
var loc += '?id=' + img;
window.location = loc;
});
function setInit(){
iu.init({
saveFile : 'store/saveImageFile'
,CallBackOnSave : function (s){
console.log("redirect");
window.location.replace("store/details");
}
});
}
iu is an image upload module which has no issues. In fact I can see the console log of redirect.
problem is, the same page gets reloaded, instead of going to the url in replace.
tried other ways like href as well, sadly the same result.
Just use:
window.location.href = "store/details";
Maybe try:
parent.window.location.href = "store/details";
You can use by this way,
Syntax
window.location.href = window.location.host + relativePath
window.location.href = relativePath
example
window.location.href = "store/details";
window.location.href = window.location.host + 'store/details';