Passing a link via viewbag MVC RAZOR &amp appearing - javascript

I'm trying to pass a link from my controller to my view using viewbag, the link is to be used as the source for an iframe. However & appears so the lnk does not work.
Controller
link = "http://example.com/Index?aa=aa&q=bb";
ViewBag.Answer3 = link;
View
<iframe height="30" id="Audio" src="">
<script>
function Reveal () {
var listen = document.getElementById("Audio");
listen.src = "#ViewBag.Answer3";
...
However it doesn't work and when I look at the debugger I see this
listen.src = "http://example.com/Index?aa=aa&q=bb";
Thanks in advance for your help
Tom

Razor automatically HTML-encodes any string you output.
You can prevent this with Html.Raw().
However, you need to Javascript-encode it instead in case it has quotes.

Related

Convert Javascript data to image source

I have the following code:
function myFunction(title) {
var searc_url = 'http://www.omdbapi.com/?apikey=cc47980e&t='.concat(title)
$.getJSON(searc_url,
function(data, textStatus, jqXHR) {
console.log(data)
console.log(data["Title"])
console.log(data["Year"])
console.log(data["imdbRating"])
console.log(data["Poster"])
document.getElementById("title").innerHTML = data["Title"]
document.getElementById("year").innerHTML = data["Year"]
document.getElementById("rating").innerHTML = data["imdbRating"]
document.getElementById("myImg").src = data["Poster"]
}
)
}
I am trying to call an image from an API, where I call:
document.getElementById("myImg").src = data["Poster"]
(The API returns the website url of the image).
Then, when I try to link this as an image in my body like so:
<img id="myImg" src='data["Poster"]' alt="The Pulpit Rock">
The image never loads. I just get the alt text. I've tried doing this, and I've tried
<img src="myImg" alt="Movie Image">
but this behaves similarly.
I've tried to google this but I'm pretty new to Javascript, HTML and PHP as a whole so it's hard to sift through the technical jargon.
Do I need to be converting this to a variable in some way that I am not??
I am just having issues displaying it on the page- not sourcing it from the API.
The is not properly getting the variable I have set (id = myImg) that contains the url.
Thanks!
EDIT: Oops. Turns out this code does work- the issue was that my browser was blocking the scripts! Oops!
Thanks everyone.
I was trying to figure out why this wasn't cooperating.
maybe if you save the value in another variable, like charge it.
var imgValue = data.poster;
and lather set that value to img attribute;
document.getElementById("myImg").src = imgValue.toString();
I hope it can help you, Good Luck!
Ooops! Resolved!
My browser was blocking my scripts.
The method above does, indeed work. Sourcing with id="myImg"

Linking bookmarklet with site's bookmark [duplicate]

Is it possible to call a javascript function from the URL? I am basically trying to leverage JS methods in a page I don't have access to the source.
Something like: http://www.example.com/mypage.aspx?javascript:printHelloWorld()
I know if you put javascript:alert("Hello World"); into the address bar it will work.
I suspect the answer to this is no but, just wondered if there was a way to do it.
There isn't from a hyperlink, no. Not unless the page has script inside specifically for this and it's checking for some parameter....but for your question, no, there's no built-in support in browsers for this.
There are however bookmarklets you can bookmark to quickly run JavaScript functions from your address bar; not sure if that meets your needs, but it's as close as it gets.
You can use Data URIs.
For example:
data:text/html,<script>alert('hi');</script>
For more information visit: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
Write in address bar
javascript:alert("hi");
Make sure you write in the beginning: javascript:
/test.html#alert('heello')
test.html
<button onClick="eval(document.location.hash.substring(1))">do it</button>
you may also place the followinng
<a href='javascript:alert("hello world!");'>Click me</a>
to your html-code, and when you click on 'Click me' hyperlink, javascript will appear in url-bar and Alert dialog will show
About the window.location.hash property:
Return the anchor part of a URL.
Example 1:
//Assume that the current URL is
var URL = "http://www.example.com/test.htm#part2";
var x = window.location.hash;
//The result of x will be:
x = "#part2"
Exmaple 2:
$(function(){
setTimeout(function(){
var id = document.location.hash;
$(id).click().blur();
}, 200);
})
Example 3:
var hash = "#search" || window.location.hash;
window.location.hash = hash;
switch(hash){
case "#search":
selectPanel("pnlSearch");
break;
case "#advsearch":
case "#admin":
}
Using Eddy's answer worked very well as I had kind of the same problem.
Just call your url with the parameters : "www.mypage.html#myAnchor"
Then, in mypage.html :
$(document).ready(function(){
var hash = window.location.hash;
if(hash.length > 0){
// your action with the hash
}
});
you can use like this situation:
for example, you have a page: http://www.example.com/page.php
then in that page.php, insert this code:
if (!empty($_GET['doaction']) && $_GET['doaction'] == blabla ){
echo '<script>alert("hello");</script>';
}
then, whenever you visit this url: http://www.example.com/page.php?doaction=blabla
then the alert will be automatically called.
Just use:
(function() {
var a = document.createElement("script");
a.type = "text/javascript";
a.src = "http://www.example.com/helloworld.js?" + Math.random();
document.getElementsByTagName("head")[0].appendChild(a)
})();
This basically creates a new JavaScript line in the head of the HTML to load the JavaScript URL you wish on the page itself. This seems more like what you were asking for. You can also change the a.src to the actual code, but for longer functions and stuff it becomes a problem. The source link can also link to a JavaScript file on your computer if targeted that way.
No; because it would make links extremely dangerous.
you can execute javascript from url via events
Ex: www.something.com/home/save?id=12<body onload="alert(1)"></body>
does work if params in url are there.
There is a Chrome extension called Bookmarklet URL (no affiliation). To append a URL with JavaScript, so that the JavaScript command is executed just after loading the webpage, one can use ?bmlet=javascript:
Example: Display an alert box
https://github.com/?bmlet=javascript:alert("Hi");
Example: Enable spell-checking while editing a GitHub README file
[Obviously, a spelling checking extension must be originally available.]
https://github.com/<username>/<repositoryname>/edit/main/README.md?bmlet=javascript:document.getElementById("code-editor").setAttribute("spellcheck","true");
On some pages, it might take some time, as the JavaScript command runs after completely loading the page. Simple commands like alert("Hi"); should run quickly.
I am a student and I have just realized my school blocked JavaScript from the address bar. It works with the "a" tag on a .html file but not on the bar anymore. I am not asking for help, I would just like to share this.
You can do one thing that is you can first open the link www.example.com. Then you can search:
javascript:window.alert("Hello World!")

Redirect to another page with JavaScript

Im developing a webapp for Android and iPhone. I need the "share in facebook" functionality, and i know i can use the url "http://www.facebook.com/sharer/sharer.php?u=", appending the url to that to open the share in Facebook window we all know and love. Im trying to do it with JavaScript, but the url that is going to be shared must be appended dynamically...
I've tried so far:
function goP(){
var fname=${promo.urlPromocion};
window.location = "http://www.facebook.com/sharer/sharer.php?u="+${object.url};
}
With quotes, without quotes, with and without the + symbol between the facebook url and the url to share, even tried to hardcode an url. I don't know if the JS function is even called. But, it doesn't work. Here is the portion of gsp file where the function is called:
<div class="rrss" style= "width:60px; height:60px; float:left; margin-right:10px; margin-left:68px;" id="facebook"><img src="../images/img_mobile/1340130521_facebook.png"/></div>
Any help?
Thank you.
Change it to
window.location.href = ....
Try:
window.location.href = 'http://example.com';
window.location is actually an object that has a property called "href".
The following script can help you.
<script type="text/javascript">
function SubmitFrm() {
var Searchtxt = $("#<%= txtSearchBooks.ClientID%>").val();
window.location = "http://www.website.com/search?sUrl=" + Searchtxt;
}
</script>
For More Details
Click Here

Getting source of iFrame

I have a file x.xhtml and another file main.html, in which I am using <iframe src=x.xhtml id=childframe>. Now what I want to do is, after the file is loaded, I want to get the source of the child frame, i.e x.xhtml, using JavaScript.
I tried the following code
function getChildInput() {
var iframe = document.getElementById('childFrame').contentWindow;
var childText = iframe.document.getElementById('childText');
alert(iframe.document.getElementsByTagName('html')[0].innerHTML);
}
but it didn't work for .xhtml. If I am using .html instead, it works fine.
Is this a problem with XHTML or is there any other way of getting the source from the child frame other than HTML?
Try alert(iframe.document.body.innerHTML);
or
var doc_iframe = document.getElementsByName("myFrame")[0].contentWindow.document;
HTH
Ivo Stoykov
Try using the documentElement property:
alert(iframe.document.documentElement.innerHTML);

Call Javascript function from URL/address bar

Is it possible to call a javascript function from the URL? I am basically trying to leverage JS methods in a page I don't have access to the source.
Something like: http://www.example.com/mypage.aspx?javascript:printHelloWorld()
I know if you put javascript:alert("Hello World"); into the address bar it will work.
I suspect the answer to this is no but, just wondered if there was a way to do it.
There isn't from a hyperlink, no. Not unless the page has script inside specifically for this and it's checking for some parameter....but for your question, no, there's no built-in support in browsers for this.
There are however bookmarklets you can bookmark to quickly run JavaScript functions from your address bar; not sure if that meets your needs, but it's as close as it gets.
You can use Data URIs.
For example:
data:text/html,<script>alert('hi');</script>
For more information visit: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
Write in address bar
javascript:alert("hi");
Make sure you write in the beginning: javascript:
/test.html#alert('heello')
test.html
<button onClick="eval(document.location.hash.substring(1))">do it</button>
you may also place the followinng
<a href='javascript:alert("hello world!");'>Click me</a>
to your html-code, and when you click on 'Click me' hyperlink, javascript will appear in url-bar and Alert dialog will show
About the window.location.hash property:
Return the anchor part of a URL.
Example 1:
//Assume that the current URL is
var URL = "http://www.example.com/test.htm#part2";
var x = window.location.hash;
//The result of x will be:
x = "#part2"
Exmaple 2:
$(function(){
setTimeout(function(){
var id = document.location.hash;
$(id).click().blur();
}, 200);
})
Example 3:
var hash = "#search" || window.location.hash;
window.location.hash = hash;
switch(hash){
case "#search":
selectPanel("pnlSearch");
break;
case "#advsearch":
case "#admin":
}
Using Eddy's answer worked very well as I had kind of the same problem.
Just call your url with the parameters : "www.mypage.html#myAnchor"
Then, in mypage.html :
$(document).ready(function(){
var hash = window.location.hash;
if(hash.length > 0){
// your action with the hash
}
});
you can use like this situation:
for example, you have a page: http://www.example.com/page.php
then in that page.php, insert this code:
if (!empty($_GET['doaction']) && $_GET['doaction'] == blabla ){
echo '<script>alert("hello");</script>';
}
then, whenever you visit this url: http://www.example.com/page.php?doaction=blabla
then the alert will be automatically called.
Just use:
(function() {
var a = document.createElement("script");
a.type = "text/javascript";
a.src = "http://www.example.com/helloworld.js?" + Math.random();
document.getElementsByTagName("head")[0].appendChild(a)
})();
This basically creates a new JavaScript line in the head of the HTML to load the JavaScript URL you wish on the page itself. This seems more like what you were asking for. You can also change the a.src to the actual code, but for longer functions and stuff it becomes a problem. The source link can also link to a JavaScript file on your computer if targeted that way.
No; because it would make links extremely dangerous.
you can execute javascript from url via events
Ex: www.something.com/home/save?id=12<body onload="alert(1)"></body>
does work if params in url are there.
There is a Chrome extension called Bookmarklet URL (no affiliation). To append a URL with JavaScript, so that the JavaScript command is executed just after loading the webpage, one can use ?bmlet=javascript:
Example: Display an alert box
https://github.com/?bmlet=javascript:alert("Hi");
Example: Enable spell-checking while editing a GitHub README file
[Obviously, a spelling checking extension must be originally available.]
https://github.com/<username>/<repositoryname>/edit/main/README.md?bmlet=javascript:document.getElementById("code-editor").setAttribute("spellcheck","true");
On some pages, it might take some time, as the JavaScript command runs after completely loading the page. Simple commands like alert("Hi"); should run quickly.
I am a student and I have just realized my school blocked JavaScript from the address bar. It works with the "a" tag on a .html file but not on the bar anymore. I am not asking for help, I would just like to share this.
You can do one thing that is you can first open the link www.example.com. Then you can search:
javascript:window.alert("Hello World!")

Categories

Resources