How can I find the title of parent window in javascript? - c#

How can I find the title of parent window in javascript?
I use
alert(window.parent.document.FormName);
and
alert(window.parent.document.title);
and
alert(window.parent.parent.document.title);
but the result wasn't true.

use this code for your solution
pageMain.html
<html>
<head>
<title>Parent Window</title>
</head>
<body>
<input type="text" id="data" value="23444" />
Open Child Popup window
</body>
<script>
function openChildWindow() {
window.open('page1.html','childWindow','width=400,height=400');
}
</script>
</html>
page1.html
<html>
<head>
<title>Child Window</title>
</head>
<body onload="initializeMainDiv();">
<div id="mainDiv"></div>
</body>
<script>
function initializeMainDiv() {
alert( window.opener.document.title )
}
</script>
</html>

You are probably trying to access the title of the parent window from an iframe which is located on another server. For security reasons it is impossible.

Related

Possible to embed javascript script in webbrowser control?

I am trying to load this javascript in a form somehow
<script type="text/javascript" src="https://ssl.gstatic.com/trends_nrtr/744_RC08/embed_loader.js"></script> <script type="text/javascript"> trends.embed.renderExploreWidget("TIMESERIES", {"comparisonItem":[{"keyword":"programming","geo":"","time":"all"}],"category":0,"property":""}, {}); </script>
The only way I could think of was to add it to an html document and load that in to a webbrowser control like so:
HTML DOC:
<HTML>
<HEAD>
<TITLE>test</TITLE>
</HEAD>
<BODY>
<script type="text/javascript" src="https://ssl.gstatic.com/trends_nrtr/744_RC08/embed_loader.js"></script> <script type="text/javascript"> trends.embed.renderExploreWidget("TIMESERIES", {"comparisonItem":[{"keyword":"dominos pizza","geo":"","time":"all"}],"category":0,"property":""}, {}); </script>
</BODY>
</html>
Code:
WebBrowser1.DocumentText = IO.File.ReadAllText("C:\Users\Zach\Desktop\test.html")
Seems simple enough but nothing loads at all.
Is this not possible or Is there a better way to accomplish what I am trying to do?

jquery simple code not working(id is assigned to the button)

This is the sample code that i am learning from w3schools. This time i have given the id too to the button. Please help.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
$("button").click(function () {
$("#div1").fadeIn();
$("#div2").fadeIn("slower");
$("#div3").fadeIn(3000);
});
});
</script>
<title></title>
</head>
<body>
<h2>Try jQuery</h2>
<br /><button>Click me</button>
<br /><br />
<div id="div1" style="width:80px;height:80px;color:green"></div>
<div id="div2" style="width:80px;height:80px;color:yellow"></div>
<div id="div3" style="width:80px;height:80px;color:blanchedalmond"></div>
</body>
</html>
$("#hide").click(function () ...
this line of code basically means "when an element with id 'hide' is clicked, do something". So your button is missing that id
<button id="hide">Click me</button>
Just copy and paste it. It works. Tested.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!--<script type="text/javascript" src="jquery-1.11.2.js"></script>-->
<script>
$(document).ready(function () {
$("#hide").click(function () {
$("p").hide();
});
});
</script>
<title></title>
</head>
<body>
<h2>Try jQuery</h2>
<p>This will get hide</p>
<button id="hide">CLick Me</button>
</body>
</html>
As others have mentioned your script is looking for something with the ID attribute 'hide' as indicated in your jQuery $('#hide').
There are many different ways to select everything in your dom. Have a look at the jquery documentation. http://api.jquery.com/category/selectors/ The simplicity of selecting dom elements is what makes jQuery so powerful in my opinion.
Select by ID:
$('#id')
You can select every even row in a table.
$('table tr:even')
You can select the 14th div element in the dom.
$('div:eq(14)')
You can select all elements with a given class.
$('.class')
Here is a JSfiddle with the code in a working fashion.
http://jsfiddle.net/5v94cnrr/
It looks like the button is ID 'hide' and it will hide all P elements on click. Typically you'd use a class called hide and hide all element with class hide on click of the button or some other action.
You have not set #hide for anything. If you need it on a button, do:
<button id="hide">CLick Me</button>
You want to hide p tag elements on onclick of button.
So firstly, you have to provide id for this button because you are making click function on button.
Try this:
<button id='hide'>Click Me</button>

Jquery code not firing inside user control aspx

I have the following Jquery in my user control which is loaded onto the page, however the jquery is not firing at all:
<script type="text/javascript" >
$(document).ready(function () {
$("div.holder").jPages({
containerID: "itemContainer",
perPage: 2,
first: false,
previous: "span.arrowPrev",
next: "span.arrowNext",
last: false
});
});
</script>
I have tried stripping this out and just using an alert when inside of document.ready but this still does not fire. How can i get this code to execute inside of a user control in aspx?
And simply this is the html i am using:
<div class="eng-container">
<!-- navigation holder -->
<div class="holder">
</div>
<!-- wrapped custom buttons for easier styling -->
<div class="customBtns">
<span class="arrowPrev"></span>
<span class="arrowNext"></span>
</div>
<ul id="itemContainer">
<li>Text</li>
<li>Text</li>
<li>Text</li>
<li>Text</li>
<li>Text</li>
</ul>
</div>
There are no obvious errors with the code i can see.
EDIT
http://jsfiddle.net/p4LES/1/
EDIT 2
Heres the pastebin link for the code, it is quite long though for the whole page. At line 185 my script begins and my HTML code related to it.
http://pastebin.com/szknh76v
UPDATE
I've tried moving the jQuery code in question to the main.Master file for my project which looks to have fixed the problem in question and it is now firing. Although I have no clue as to why the javascript would not fire within the control.
I created a new HTML page and this worked for me:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="js/jPages.min.js"></script>
<script type="text/javascript" >
$(document).ready(function () {
$("div.holder").jPages({
containerID: "itemContainer",
perPage: 2,
first: false,
previous: "span.arrowPrev",
next: "span.arrowNext",
last: false
});
alert("myMsg");
});
</script>
<head>
<title></title>
</head>
<body>
<div class="eng-container">
<!-- navigation holder -->
<div class="holder">
</div>
<!-- wrapped custom buttons for easier styling -->
<div class="customBtns">
<span class="arrowPrev"></span>
<span class="arrowNext"></span>
</div>
<ul id="itemContainer">
<li>Text</li>
<li>Text</li>
<li>Text</li>
<li>Text</li>
<li>Text</li>
</ul>
</div>
</body>
</html>

JQuery don’t work in aspx-page with Masterpage

I have made this example and it works fine on a plain aspx webpage. I use Visual Studio 2010.
Head-part:
<title>Show/hide element</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#CheckBoxShowHide').click(function () {
$("#ShowHideElement").slideToggle();
});
});
</script>
<style type="text/css">
#ShowHideElement
{
width:400px;
height:100px;
background-color:Aqua;
}
</style>
Body-part:
<form id="form1" runat="server">
<asp:CheckBox ID="CheckBoxShowHide" runat="server" Text="Show/hide" />
<div id="ShowHideElement">
This is the element for show/hide
</div>
</form>
When I have a masterpage and the same code on the child webpage JQuery dosent work. The loading of the JQuery javascript file fails. The child page and the masterpage are in the same folder. If I put the code on the masterpage it works fine but I want JQuery on the child page too. Please help me.
I can see another problem as well, you are trying to grab the checkbox ID based on its server ID not ClientID. Once a asp control has been rendered onto the client its ID gets changed. Try the following code:
<title>Show/hide element</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#<%=CheckBoxShowHide.ClientID%>').click(function () {
$("#ShowHideElement").slideToggle();
});
});
</script>
<style type="text/css">
#ShowHideElement
{
width:400px;
height:100px;
background-color:Aqua;
}
</style>
Body-part:
<form id="form1" runat="server">
<asp:CheckBox ID="CheckBoxShowHide" runat="server" Text="Show/hide" />
<div id="ShowHideElement">
This is the element for show/hide
</div>
</form>
The following line is the only thing I changed:
$('#<%=CheckBoxShowHide.ClientID%>').click(function () {
Hope it helps.
Are you sure your page is loading jQuery, use a absolute URL in your master page to reference the jQuery library.
If jQuery is on your masterpage, it will work on your child page.
Master <head>
<head>
<script type="text/javascript" src="js/jquery.js"></script>
</head>
Child <head>
<head>
<script type="text/javascript">
$(document).ready(function () {
//Do Child jQuery Stuff here....
});
</script>
<head>
If you are having issues the only other thing to check is to make sure that your path to the jquery file is right. (ie Maybe it should be ../js/jquery.js)
Use this to make sure that isn't the issue if the other thing I suggested doesn't work:
For your Master Page <head>:
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
Or (if you want to host it)
<head>
<script type="text/javascript" src='<%=ResolveURL("~/js/jquery.js")%>'></script>
</head>
Where ~/ is your root.
You should be able to just place the link to the JQuery library in the HEAD section of the master page. When the page is ran it will generate the HTML content for the master page with the link in the HEAD section, the content page should be able to then make user of the JQuery library. I know we had an issue with how the link was being done. Maybe try linking in the HEAD of the master page like this instead:
<script type="text/javascript" src='<% = ResolveURL("~/js/jquery.js") %>' ></script>
The '<% %>' is a way to do inline server side code as the page loads, so the page will inject the correct src given the location of the URL.

Using Jquery not able to get correct out put

I did a small style changing app using Jquery in Asp.Net 3.5 .I am having IE7.
When i click on button the paragraph color should change.
Problem is ,the color is changing,it is not stable.Means just like blinking.
Please find the code below
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript" src="scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#Button1").click(function() {
$("p").css("background-color", "Yellow")
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" />
<p>This Should be in <br/>
yellow</p>
</div>
</form>
</body>
</html>
Its because ASP.NET changes the ID attribute to be something it can resolve.
If you are using ASP.NET 4, set the ClientIDMode to AutoID.
If you are using anything else, change your selector to use "#<%= Button1.ClientID %>"
Also, return false to prevent the form from being postbacked.
try this :
$("#<%=Button1.ClientID %>").click(function() {
$("p").css("background-color", "Yellow")
});
When the Button renders on the client it won't have id = "Button1"
Change:
$("#Button1").click(function() {
to
$("#<%=Button1.ClientID%>").click(function() {
$("input[id$='Button1']").click(function() {
button click stuff here.
});

Categories

Resources