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];
Related
<script>
var state = 'Done';
</script>
how to pass javascript variable state into the django url in the line above when it change
Try with something like this:
var stateVar = 'Done'
var url = "{% url 'main:projectdata' proname=pro.projectname n=state %}".replace('state', stateVar )
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>
I am new and just doing practices i just did:
var i= [["j.php?i=1"]]; and it sent value to php and prints but when i want some input from user it does nothing. For example:
file j.js
var input = "hello";
var send= [["j.php?i="+input]];
and in j.php
<?php
$e=$_GET['i'];
echo $e;
?>
Any idea or i am totally wrong? I am trying to have some variation. And i really did with window.location.href. But I just want to know how to do in this way var send= [[]]. Thanks
Edit:
your question is not very clear, maybe try this:
var input = "hello";
var url = "/j.php?i=" + input;
var send = [[url]];
But, it's not clear to me what you want to do with your send variable...
Original
You have to send the data to the server either using a a tag (GET method), a form (POST method) or an Ajax request (any Http verb):
<!-- Here you give a unique id to your link -->
<a id="link" href="">your link</a>
<script>
var i = "Hello";
// wait for page to be loaded
document.addEventListener("DOMContentLoaded", function(event) {
// change the href attribute of the link with the id equal to 'link'
// with whatever data contained in the i variable
// this is done after the page is loaded
document.getElementById("link").href = "/j.php?i=" + i;
});
</script>
Then, when you just click the link, it will send the variable named i containgin the value Hello to your php page.
You could also do it in this way:
<a id="link2">your link</a>
<script>
var i = "HELLO";
// wait for page to be loaded
document.addEventListener("DOMContentLoaded", function(event) {
// here we are telling javascript to change the url
// in the browser when the user clicks the link
document.getElementById('link2').onclick = function() {
window.location = '/j.php?i=' + i
}
});
</script>
I'm trying to change the URL in the address bar using javascript.
So if the user access the page using
www.example.com/ajax/project8.html
Url should be changed automatically to
www.examp.com/#cbp=ajax/project8.html
shouldn't be any harder than this:
window.location = "http://whatever.you.want.com"
UPDATE
So you want your site to redirect to another page when the url is www.example.com/ajax/project.aspx?id=whatever and id=xxx could be any id.
To achieve that you need a function that returns the query string parameter value eg:id=whatever
Then check if the current url needs to be redirected to another page. If this is the case then redirect to new url with same parameter value.
/*
function that returns a query string parameter value
this function works with many parameters
Eg: www.example.com/#cbp=ajax/project.aspx?myParam=hello&id=1283&otherParam=234
to get the param value just give it the parameters name
getQueryStringValue("id") returns : 1283
getQueryStringValue("myParam") returns : "hello"
*/
function getQueryStringValue( name ){
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
//current url
var currentUrl = location.href;
//check if current url contains www.example.com/ajax/project.aspx
if (currentUrl.indexOf("www.example.com/ajax/project.aspx") != -1 ){
//new url for redirection
var newUrl = "www.example.com/#cbp=ajax/project.aspx?id=" + getQueryStringValue( "id" );
//redirect to new page
location.href = newUrl;
}
Try this code
if (window.location.href == 'www.example.com/ajax/project8.html') {
window.location = 'www.examp.com/#cbp=ajax/project8.html';
}
you can set all things like
window.location.href = "www.examp.com/#cbp=ajax/project8.html"
for more details how you will manage all url parameter then please see
JavaScript and jQuery url managment
window.location.href = "#cbp=ajax/project8.html";
you can change the value written after # to any location , div id etc.
e.g
window.location.href = "#myDivID";
<meta http-equiv="refresh" content="0; url=http://example.com/" />
Note: please put on header
or
<script type="text/javascript">
window.location.assign("http://www.example.com")
</script>
I am using following code to append param to url. This is working fine but when parameter is appended in url, page is getting reloaded. I want to use this functionality without reloading the page .
function insertParam(key, value)
{
key = escape(key); value = escape(value);
var kvp = document.location.search.substr(1).split('&');
var i=kvp.length; var x; while(i--)
{
x = kvp[i].split('=');
if (x[0]==key)
{
x[1] = value;
kvp[i] = x.join('=');
alert('sdfadsf');
break;
}
}
if(i<0) {kvp[kvp.length] = [key,value].join('=');}
//this will reload the page, it's likely better to store this until finished
document.location.search = kvp.join('&');
//alert(document.location.href);
}
I want to add multiple params to url without reloading the page like:
txt1
txt2
txt3
link1
link2
link3
i want url : "..../search.php"
after click on txt2
i want url : "..../search.php#t_2"
after click on link2
i want url : "..../search.php#t_1&l_2"
You can only do this using history.pushState(state, title, url) which is an HTML5 feature.
There is a new feature that aims to replace the use of location.hash with a better solution: pushState.
window.history.pushState(data, "Title", "/new-url");
More information: http://badassjs.com/post/840846392/location-hash-is-dead-long-live-html5-pushstate