Url.Action is generating non cross browser friendly urls - javascript

URl.Action is generating a query string in a way that works on IE8, but not on Chrome when a date is being passed.
Here is our code.
function RunReport( PdfOrExcel)
{
var ChartType = "Pdf";
var argCounter = 0;
linkUrl = '#Url.Action("ClassAssignmentLoadSummaryReport", "ReportsScheduling", new { PdfOrExcel="[1]", RptDate="[2]" } )';
var objToSend = new Object();
value = $('#RptDate').val()
dataToSend.RptDate =value;
linkUrl = linkUrl.replace("%5B1%5D", PdfOrExcel);
linkUrl = linkUrl.replace("%5B2%5D", value );
w = window.open(linkurl);
w.focus();
}
(this is a little ugly becuase we unwound several function to get the code above)
It generates a url like this:
/appName/ReportsScheduling/ClassAssignmentLoadSummaryReport?PdfOrExcel=Pdf&RptDate=8/6/2012
If we change it like this it works in Chrome just fine.
/appName/ReportsScheduling/ClassAssignmentLoadSummaryReport?PdfOrExcel=Pdf&RptDate=8/6/2012
I assume we are doing something dumb and it's generating it this way because of that, but I can't figure out what We're doing wrong.
Any help is greatly appreciated.

It looks like your string is getting encoded. Try wrapping the Url.Action() call with #Html.Raw().

If it's really getting that messy..
Why not just use..
var href = "/ReportsScheduling/ClassAssignmentLoadSummaryReport?PdfOrExcel=blah&RptDate=blahDate"

Related

Extracting img urls from webpage using Google Apps Script

This is an Apps Script that goes through a webpage and collects img urls that are inside some div of a special class.
function getIMGs(url){
var url = 'url'
var result = UrlFetchApp.fetch(url);
if (result.getResponseCode() == 200) {
var doc = Xml.parse(result, true);
var bodyHtml = doc.html.body.toXmlString();
var doc = XmlService.parse(bodyHtml);
var html = doc.getRootElement();
var thumbs = getElementsByClassName(html, 'thumb');
var sheet = SpreadsheetApp.getActiveSheet();
for (i in Thumbs) {
var output = '';
var linksInMenu = getElementsByTagName(thumbs[i], 'img');
for(i in linksInMenu) {
output += XmlService.getRawFormat().format(linksInMenu[i]);
}
var linkRegExp = /data-src="(.*?)"/;
var dataSrc = linkRegExp.exec(output);
sheet.appendRow([dataSrc[1]]);
}
}
So first the code gets the html, and uses an auxiliary function to get certain elements, which look like this:
<div class="thumb"><div class="loader"><span class="icon-uniE611"></span></div><img src="//xxx" data-src="https://xxx/8491a83b1cacc2401907997b5b93e433c03c91f.JPG" data-target="#image-slider" data-slide-to="0"></div>
Then the code gets the img elements, and finally extracts the data-src address via RegExp.
While this kinda works, I have a problem:
1) After 9 loops it crashes, on the appendRow line, as the last 4 Thumbs elements don't have data-src, hence what i'm trying to write into the spreadsheet is null.
Any solution for this? I have fixed it for the moment by just doing 9 iterations only of the For loop, but this is far from optimal, as it's not automated and required me to go through the page to count the elements with data-src.
Also, any suggestion of a more elegant solution will be appreciated! I will be really grateful for any helping hand!
Cheers

put string as javascript object

i've the following object and I need to put the "string",how should I put the value of name
{"name":{"_parent":["/test"]}}
inside,I try with " which doesnt work for me ,any idea ?
var file = {
"name" : "{"name":{"_parent":["/test"]}}"
update
I cannot use the jsonParse or stringify as I need to put it hardcoded
Your question is a bit unclear but I think you want to do this
The reason why JSON.parse() is not working for you is because you are missing the single quotes.
var yourString = '{"_parent":["/test"]}'
var file = '{"name" : {"name":{"_parent":["/test"]}}}'
var obj = JSON.parse(file);
obj.name.name = yourString;
var backToString = JSON.stringify(obj);
Use this :
JSON.parse(file);
For more information you could look in this site :
http://www.w3schools.com/js/js_json.asp

Javascript - Adding variable to a string of code

I realized how bad I am with javascript :(
I have this:
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,
function(m,key,value) {
vars[key] = value;
});
return vars;
}
var theidis = getUrlVars()["id"];
and
$.tzPOST = function(action,data,callback){
$.post('php/ajax.php?id='+theidis,'&action='+action,data,callback,'json');
}
$.tzGET = function(action,data,callback){
$.get('php/ajax.php?id='+theidis,'&action='+action,data,callback,'json');
}
The first gets the ?id=value parameter properly. I tested with an alert(theidis).
I am looking to do a simple thing - sorry if this is stupid -.- I have worked as this for so long that I am beginning to oversee things.
I want an ?id=value to be added to the php/ajax.php. I tried with the above, but it doesn't seem to work. I also tried by just adding it.
Thank you in advance. :-)
Your $.get call specifies
'php/ajax.php?id='+theidis, '&action='+action, data ...
It looks like you're passing data twice. Either &action='+action should be your data parameter, or data. Did you mean to concatenate the &action part into the URL?
you probably meant:
$.tzGET = function(action,data,callback){
$.get('php/ajax.php?id='+theidis + '&action='+action,data,callback,'json');
}
Change the , sign to + sign.
NOTE!: data should be null if you are cocncating string to the URL's query string.

Javascript for google image ripping broke with update

I grabbed a few small scripts and threw them together to take google's new image layout and turn back into the old one, then take the images and replace them with the full size versions. Worked great until about last week. Not sure what changed on the server side.
(function() {
// Get list of all anchor tags that have an href attribute containing the start and stop key strings.
var fullImgUrls = selectNodes(document, document.body, "//a[contains(#href,'/imgres?imgurl\x3d')][contains(#href,'\x26imgrefurl=')]");
//clear existing markup
var imgContent = document.getElementById('ImgContent');
imgContent.innerHTML = "";
for(var x=1; x<=fullImgUrls.length; x++) {
//reverse X to show images in correct order using .insertBefore imgContent.nextSibling
var reversedX = (fullImgUrls.length) - x;
// get url using regexp
var fullUrl = fullImgUrls[reversedX].href.match( /\/imgres\?imgurl\=(.*?)\&imgrefurl\=(.*?)\&usg/ );
// if url was fetched, create img with fullUrl src
if(fullUrl) {
newLink = document.createElement('a');
imgContent.parentNode.insertBefore(newLink , imgContent.nextSibling);
newLink.href = unescape(fullUrl[2]);
newElement = document.createElement('img');
newLink.appendChild(newElement);
newElement.src = decodeURI(fullUrl[1]);
newElement.border = 0;
newElement.title = fullUrl[2];
}
}
function selectNodes(document, context, xpath) {
var nodes = document.evaluate(xpath, context, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
var result = [];
for (var x=0; x<nodes.snapshotLength; x++) {
result.push(nodes.snapshotItem(x));
}
return result;
}
})();
Google changed the 'ImgContent' id for the image table holder to something slightly more obscure. A quick change had everything working again. I made a simple problem complicated by looking past the easy stuff. Thanks to darvids0n for the enabling, he ultimately pointed out what I was missing.
the script is not going to work as said by bobby .
try this grease monkey script from user script repository.
rip Google image search :- http://userscripts.org/scripts/show/111342

Using a Javascript Variable & Sending to JSON

I'm trying to take a URL's hash value, send it through a function, turn that value into an object, but ultimately send the value to JSON. I have the following setup:
function content(cur){
var mycur = $H(cur);
var pars = "p="+mycur.toJSON();
new Ajax.Updater('my_box', 'test.php', {
parameters: pars
});
}
function update(){
if(window.location.hash.length > 0){
content(window.location.hash.substr(1)); // Everything after the '#'
}
}
var curHashVal = window.location.hash;
window.onload = function(){
setInterval(function(){
if(curHashVal != window.location.hash){
update();
curHashVal = window.location.hash;
}
},1);
}
But for some reason, I can't seem to get the right JSON output. It will either return as a very large object (1:"{",2:"k") or not return at all. I doubt that it is impossible to accomplish, but I've exhausted most of the ways I can think of.
Other ways I've tried were "{" + cur + "}" as well as cur.toObject(), however, none seemed to get the job done.
Thanks for the help!
EDIT: As an end result, I'd like the URL (say product:3,confirmed:1) to be returned as {"product":3,"confirmed":1}
A typical implementation of toJSON() needs either an Array or an Object as the top-most element. Sonsomethig like this will probably work:
var pars = {p: myCur};
pars = pars.toJSON();
First of all native JSON support and the toJSONmethod is not available in all browsers. Older browsers like IE 6/7 or Firefox 2/3 do not support it. You should use a JSON library like json2 by Douglas Crockford. Second I would suggest to use the stringify method of the global JSON object instead of the toJSON function. In my tests
JSON.stringify("...")
works just fine.
If anyone is still looking for my answer, here's the solution!
function build_json(str){
var new_str = "{";
var cut = str.split(",");
for(var x=0; x < cut.length; x++){
if(x > 0){new_str = new_str+",";}
var s = cut[x].split(":");
new_str = new_str+"\""+s[0]+"\":\""+s[1]+"\"";
}
return new_str+"}";
}

Categories

Resources