Query String - Multiple parameters - html

I am trying to pass the following Query String but am being met with a Webpage not available error:
window.location.href = "chaindeals.html?chainID=" + chainID + "&imageURL=" + imageURL;
I also tried changing the & to & but I'm getting the same error.
Would anyone know what I am doing wrong?
Thanks for your help.

please try this.
var url = '';
url = url.concat('chaindeals.html?chainID=',chainID, '&imageURL=', imageURL);
window.location.href = url;

Try to give the full url to window.location.href, not only the page name :
var host = "http://" + window.location.host;
window.location.href =
host
+ "/chaindeals.html"
+ "?chainID=" + chainID
+ "&imageURL=" + imageURL;

Related

MVC - model to a json object

I had a mvc object as my model.
I need to stringfiy my model as a json object type - and then use it in js as i please.
i currently doing something like this
<script type="text/javascript">
$(function () {
var jsonData2 = '#Html.Raw(Json.Encode(Model))';
showBeginDate(jsonData2);
});
</script>
But when i try to acess a json property for exemple as jsonData2.BeginDate I keep getting undefined.
jsonData2 is a json object - why can i "read" from it?
Regards
#riteshmeher 's suggestion is correct
var text = '#Html.Raw(Json.Encode(Model))';
var obj = JSON.parse(text);
I don't know your model so I created a simple model with 2 attributes: Id and Name. In case that the model is a list, you can read it:
// Access to object in position 1
var result = obj[0].Id + " - " + obj[0].Name;
In other case, access right to the property.
var result = obj.Id + " - " + obj.Name;
For more info, check this post:
http://www.w3schools.com/js/js_properties.asp
http://www.w3schools.com/json/tryit.asp?filename=tryjson_parse
UPDATE
like #Stephen Muecke said, better this:
var obj = #Html.Raw(Json.Encode(Model));
var result = obj[0].Id + " - " + obj[0].Name;
thank #Craig for the corrections

Trying to restart my Twitch API from scratch. Not sure how to get my response

I'm in the process of redesigning my website, and with this I wanted to update/improve the various scripts involved. One such script is a call to the Twitch API for a set group of streamers, then displaying who's online and, if any are, displaying the first live stream and allow the user to switch between them.
My original script, which works great, was originally constructed by somebody here on StackOverflow. The problem is that instead of guiding me to a usable answer, they just rewrote my code to work in a way that I have a hard time following. I honestly have no idea how the loop they made works. It works, and I use it now, but that's my mistake. I want to start fresh and get a return for each user in an array, then construct things out based on the results of their live status. I don't understand how it is currently being done with this code, and I can't seem to figure out the point where it is saving the return, so I can't properly log it to the console to look at it and use it proper.
Long story short, can anybody help me with a simple return in a loop for each user so I can log it to the console and look at it and use it DIRECTLY instead of saving pieces to a million variables? Here's the code I'm currently using:
$(document).ready(function () {
var casters = ["marcusraven86","whit3rabbit87","dragonbornmonocle","m3ta1head","jimmyjojo7","redshoes","sergeantatams","stevechopz"];
var castersLive = [];
var reqs = $.map(casters, function (userName, i) {
return $.getJSON('https://api.twitch.tv/kraken/streams/' + userName + '.json?callback=?');
})
$.when.apply($, reqs).done(function () {
$.each(arguments, function (i, array) {
var data = array[0];
var userName = casters[i];
console.log(data);
if (data.stream === null) {
$('<div class="streampost"></div>').append(
'<a id="offline' + userName + '" class = "offline" href="http://www.twitch.tv/' + userName + '/profile" title="' + userName + '" target="_blank"></a>').appendTo('#statusblock');
}
else {
castersLive.push(casters[i]);
$('<div class="streampost"></div>').append(
'<a id="online' + userName + '" class = "online" href="http://player.twitch.tv/?channel=' + userName + '" title="' + userName + '" target="stream"></a>').appendTo('#statusblock');
}
});
console.log(castersLive);
if (castersLive.length > 0) {
$('<div class="streamplayer embed-responsive embed-responsive-16by9"></div>').append('<iframe src="http://player.twitch.tv/?channel=' + castersLive[0] +'" name="stream"></iframe><iframe src="http://www.twitch.tv/' + castersLive[0] + '/chat?popout=" frameborder="0" scrolling="no" height="478" width="350" id="chatFrame" name="chatFrame"></iframe>').appendTo('#streamblock');
}
else {}
$('.online').click(function() {
var streamName = $(this).attr('title');
$('#chatFrame').attr('src', 'http://www.twitch.tv/' + streamName + '/chat?popout=');
});
});
});
And here is where I'm trying to restart, but I can't seem to get the call to the API correct. I'm still a bit new to all this, and I also use the Tumblr and YouTube APIs. Each is different, and it throws me off a little.
$(document).ready(function () {
var casters = ["marcusraven86","whit3rabbit87","dragonbornmonocle","m3ta1head","jimmyjojo7","redshoes","sergeantatams","stevechopz"];
var castersLive = [];
for (i=0; i <= casters.length; i++) {
$.get('https://api.twitch.tv/kraken/streams/' + casters[i] + '.json?callback=?'), function ( data ) {
console.log(data);
};
};

Add Extension to Index File

I've been searching online for an answer to this but I can't seem to find anything which can help.
I was wondering if its possible to open a index.html file with an extension after the .html
(for example it would open a file like this - index.html?lc=uk) automatically when you would double click the file or when you click on a link which connects to that file.
Hope that makes sense.
If anyone could help would be much appreciated.
Regards,
Seb
user2072826
function setGetParameter(paramName, paramValue)
{
var url = window.location.href;
if (url.indexOf(paramName + "=") >= 0)
{
var prefix = url.substring(0, url.indexOf(paramName));
var suffix = url.substring(url.indexOf(paramName));
suffix = suffix.substring(suffix.indexOf("=") + 1);
suffix = (suffix.indexOf("&") >= 0) ? suffix.substring(suffix.indexOf("&")) : "";
url = prefix + paramName + "=" + paramValue + suffix;
}
else
{
if (url.indexOf("?") < 0)
url += "?" + paramName + "=" + paramValue;
else
url += "&" + paramName + "=" + paramValue;
}
window.location.href = url;
}
and then in the body tag:
<body onload="setGetParameter('lc', 'uk');">
This has worked but the problem is that it keeps refreshing the page constantly. Is there a way to stop the refreshing?
Unfortunately this does not work. You can not pass URL Parameters into the file-name.
If you want to add it as the page loads, you could add this to the JavaScript of the page:
function setGetParameter(paramName, paramValue)
{
var url = window.location.href;
if(!(url.indexOf(paramName) >= 0))
{
if (url.indexOf(paramName + "=") >= 0)
{
var prefix = url.substring(0, url.indexOf(paramName));
var suffix = url.substring(url.indexOf(paramName));
suffix = suffix.substring(suffix.indexOf("=") + 1);
suffix = (suffix.indexOf("&") >= 0) ? suffix.substring(suffix.indexOf("&")) : "";
url = prefix + paramName + "=" + paramValue + suffix;
}
else
{
if (url.indexOf("?") < 0)
url += "?" + paramName + "=" + paramValue;
else
url += "&" + paramName + "=" + paramValue;
}
window.location.href = url;
}
}
and then in the body tag:
<body onload="setGetParameter('lc', 'uk');">
Original Source(Yes, there is a difference between the code)
If you are using Apache, you may use mod_rewrite to perform almost any kind of "magic" with request file names. As long as you know home to use regular ecpressions, of course. I suggest you to do some research on mod_rewrite.

JSONP read text from exernal file

Please help
I have external link with JSON object's , link: http://creativescream.com/html5ws/api/news.
Now i must to read json on my site but i can;t do that. I used to make my json file and that work ok but that is not a poent i must read JSON from external link.
I tried this code but it will not work:
<script>
$.getJSON("http://creativescream.com/html5ws/api/news",
function(json){
for(i=0; i<3; i++){
var newsTitle = json.contents[i].title;
var newsContent = json.contents[i].news;
var newsAuthor = json.contents[i].author;
var newsLink = json.contents[i].url;
$("#latest_news").append("<h2>" + newsTitle + "</h2><br />");
$("#latest_news").append("<p>" + newsContent + "</p><br /><a href='" + newsLink + "' target='_blank'>[Pročitaj opširnije]</a><br />");
$("#latest_news").append("<span style='color: #000000; font-weight: bold;margin-left:10px; '>Author: " + newsAuthor + "</span><br /><br />");
}
});
</script>
Please help me.
The site isn't returning jsonp nor is jquery treating it as such. Add ?callback=? to the end of the URL in order for jquery to treat it like jsonp and tell the server the function name to inject into the response.

Parsing a string, using action script

I am working on a flex site,
I want to parse the string below:
http://virtual.s1.c7beta.com/rpc/raw?c=Pictures&m=download_picture&key=a4fb33241662c35fe0b46c7e40a5b416&session_id=2b7075f175f599b9390dd06f7b724ee7
and remove the &session_id=2b7075f175f599b9390dd06f7b724ee7 from it.
How should i do it.
also later on when i get the url from database without session_id, i will attach the new session_id to it. Please tell me how to do that too.
Regards
Zeeshan
Not the short version but you can use URLVariable to get all the field(name, value) into an object and then add or delete any field you want.
Separate url and query:
var url : String = "http://virtual.s1.c7beta.com/rpc/raw?c=Pictures&m=download%5Fpicture&key=a4fb33241662c35fe0b46c7e40a5b416&session%5Fid=2b7075f175f599b9390dd06f7b724ee7";
var idxQMark : int = url.indexOf("?");
var qry : String = "";
Build url variable from query
var uv : URLVariables = new URLVariables();
if (idxQMark > 0) {
qry = url.substr(idxQMark + 1);
url = url.substr(0, idxQMark);
uv.decode(qry);
}
NOw you can access whatever field you need or delete them
delete uv.session_id;
Rebuild the query when needed
qry = uv.toString();
// get back you new URL
if (qry != "") {
url = url + "?" + qry;
}
For URI parsing check out the URI class provided in as3corelib.
http://code.google.com/p/as3corelib/source/browse/trunk/src/com/adobe/net/URI.as
For your specific case, you can clear the parts of the URI you don't want.
var url : String = "http://virtual.s1.c7beta.com/rpc/raw?c=Pictures&m=download%5Fpicture&key=a4fb33241662c35fe0b46c7e40a5b416&session%5Fid=2b7075f175f599b9390dd06f7b724ee7";
var uri : URI = new URI(url);
uri.query = "";
uri.fragment = "";
var shortenedUrl : String = uri.toString();
Or if you know you want to construct your url from just the scheme, authority, and path (i.e., you don't need to preserve port, username, password, etc.) you can build the url from parts.
var shortenedUrl : String = uri.scheme + "://" + uri.authority + "/" + uri.path;
If you're not sure that sessionid will be last you could split on the & using the as3 string.split functionhttp://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/String.html#split%28%29
then rebuild the string using a for loop leaving out strings that start with session_id using the string.indexOf function http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/String.html#indexOf%28%29 to see if "session_id" is at index 0
You can use regular expressions in Flex ... here's an example.
Assuming the session ID will always be the last variable in the query string:
var newStr:String = myStr.slice(0, myStr.indexOf("&session"));