I want to preview some HTML code in an HTML page. but when I do this, the browser treats it as a actual HTML, instead of just previewing it (including its tags for example). How can I prevent this from happening?
You may think of this page as a tutorial to HTML that wants to present some HTML examples to the learners.
Turn the < signs to < and the > signs to >.
htmlspecialchars() will do this for you in PHP.
Put the HTML code between <xmp></xmp> tags.
Here's a JavaScript module I wrote for writing a report. The user can enter HTML or Markdown in a textarea, and then see the finished result, and toggle between them.
function Report (id) {
this.div = gel(id);
this.div.style.height = '100%';
this.trip;
}
//initializes the Report object and renders the HTML of the report to this.div
Report.prototype.init = function(trip) {
this.report = {'author':trip.owner,
'time_created':trip.time_created,
'title':trip.title,
'text':trip.summary,
'url':trip.url,
'category':trip.category,
'id':trip.id}
this.div.appendChild(this.renderHTML());
}
//show the edit form when the user clicks the edit link
Report.prototype.editForm = function() {
var r = this.report;
var form = dec('form');
form.action = '/trips/submit_link';
form.method = 'POST';
var t = dec('table');
t.style.width ='95%';
t.style.paddingBottom ='10px';
var tr = dec('tr');
t.appendChild(tr);
var td = dec('td');
td.style.width = '15%';
var strong = dec('strong');
strong.appendChild(dct('Title'));
td.appendChild(strong);
tr.appendChild(td);
td = dec('td');
td.style.width = '100%';
var input = dec('input');
input.type = 'text';
input.style.width = '100%';
input.className = "required"
input.name = 'reportTitle';
input.value = r.title;
td.appendChild(input);
tr.appendChild(td);
tr = dec('tr');
t.appendChild(tr);
td = dec('td');
var input = dec('input');
var strong = dec('strong');
strong.appendChild(dct('URL '));
td.appendChild(strong);
tr.appendChild(td);
td = dec('td');
input.type = 'text';
input.name = 'reportURL';
//input.className = "validate-url"
input.value = r.url;
input.style.width = '100%';
td.appendChild(input);
tr.appendChild(td);
form.appendChild(t);
var strong = dec('strong');
strong.appendChild(dct('Summary or Report '));
form.appendChild(strong);
var input = dec('textarea');
input.style.width = '96%';
input.style.height = '350px';
input.name = 'reportBody';
var converter = new Showdown.converter();
input.innerHTML = converter.makeHtml(r.text);
input.style.marginTop = "5px";
input.style.marginBottom = "5px";
form.appendChild(input);
var strong = dec('strong');
strong.appendChild(dct('Category'));
strong.style.marginRight = '10px';
form.appendChild(strong);
var input = dec('input');
input.type = 'text';
input.name = 'category';
input.style.width = '75%';
input.value = r.category;
form.appendChild(input);
form.appendChild(dec('br'));
form.appendChild(dec('br'));
input = dec('input');
input.type = 'submit';
input.value = 'Save';
input.style.cssFloat = 'left';
input.style.styleFloat = 'left';
input.style.fontWeight = 'bold';
form.appendChild(input);
input = dec('input');
input.type = 'button';
input.value = 'Cancel';
input.style.color = '#cc5500';
input.style.fontWeight = 'bold';
input.style.cssFloat = 'right';
input.style.styleFloat = 'right';
var self = this;
input.onclick = function() {
rac(self.div);
self.div.appendChild(self.renderHTML());
}
form.appendChild(input);
input = dec('input');
input.type = 'hidden';
input.name = 'tripID';
input.value = tripID;
form.appendChild(input);
return form;
}
//renders the HTML of the report to this.div
Report.prototype.renderHTML = function() {
var report = this.report
var rDiv = dec('div');
rDiv.style.height = '100%';
var h1 = dec ('h1')
if (report.url) {
var a = dec('a')
a.href = report.url;
a.target = '_blank';
a.appendChild(dct(report.title));
h1.appendChild(a);
var img = dec('img');
img.src = '/site_media/images/external_link.gif';
img.style.marginLeft = '5px';
h1.appendChild(img);
} else {
h1.appendChild(dct(report.title));
}
if (report.author == user) {
var a = dec('a');
var self = this;
a.onclick = function() {
rac(self.div);
self.div.appendChild(self.editForm());
attachToForms();
}
a.className = 'jLink';
a.appendChild(dct('(edit)'));
h1.appendChild(dct(' '));
h1.appendChild(a);
}
rDiv.appendChild(h1);
rDiv.appendChild(dct('Posted on '));
rDiv.appendChild(dct(report.time_created));
rDiv.appendChild(dct(' by '));
var a = dec('a');
a.href = '/user/' + report.author;
a.appendChild(dct(report.author));
rDiv.appendChild(a);
if (report.url) {
var div = dec('div');
div.style.color = 'green';
div.style.fontSize = '.8em';
div.appendChild(dct(report.url));
rDiv.appendChild(div);
}
rDiv.appendChild(dec('hr'));
var div = dec('div');
div.style.height = '75%';
div.style.overflowY = 'auto';
if (report.text) {
var converter = new Showdown.converter();
div.innerHTML = converter.makeHtml(report.text);
}
rDiv.appendChild(div)
rac(this.div)
this.div.appendChild(rDiv)
return rDiv;
}
I'm not sure what you mean by 'previewing' the html code, since you're seeing it when you are typing it into your text editor. Perhaps you're looking for the "View Source" option in a web browser when visiting a page?
If you are looking to prevent a browser from 'eating' your html code and interpretting it, and are interested in seeing the code rather than rendering it, you can use htmlspecialchars().
Related
I am working on an application that initiates a popup dialog, this dialog requires a session however it is getting lost. This is happening on Chrome only so far. I've test in Opera and Edge, both are working. I am only losing the session in Chrome. I submitted this question in SuperUsers site as well. Apologies for the additional code, it's probably not necessary.
Below is a jsfiddle that produces a dialog.
https://jsfiddle.net/0kv4bqjy/2/
Html
<div id="apollo-actions-wrapper" data-uri="www.sap.com">
<div id="apollo-inner-wrapper">
<img src="https://www.apolloproject.xyz/a/images/apollo-launcher.png"/>
<span id="apollo-likes"><span id="likes-span"></span> likes</span>
<span id="apollo-shares"><span id="shares-span"></span> shares</span>
<span id="apollo">Apollo</span>
</div>
</div>
Javascript
var ResourceRequest = function(){
var http = function(uri, method){
var request = new XMLHttpRequest();
request.onprogress = updateRequestProgress;
return new Promise(function(resolve, reject){
request.onreadystatechange = function () {
if (request.readyState !== 4) return;
if (request.status >= 200 && request.status < 300) {
resolve(request);
} else {
reject({
status: request.status,
statusText: request.statusText
});
}
};
request.open(method ? method : 'get', uri, true);
request.send();
});
}
var updateRequestProgress = function(arg){
}
return {
http: http
}
}
var popup = {}
var actionsWrapper = document.getElementById("apollo-actions-wrapper")
var innerActionsWrapper = document.getElementById("apollo-inner-wrapper");
var launchApollo = document.getElementById("launch-apollo")
var likesWrapper = document.getElementById("apollo-likes")
var sharesWrapper = document.getElementById("apollo-shares")
var likesSpan = document.getElementById("likes-span")
var sharesSpan = document.getElementById("shares-span")
var apolloSpan = document.getElementById("apollo")
addActionStyles()
var req = new ResourceRequest()
var uri = actionsWrapper.getAttribute("data-uri")
var likesUri = "https://www.apolloproject.xyz/a/resource/likes?uri=" + encodeURIComponent(uri);
var sharesUri = "https://www.apolloproject.xyz/a/resource/shares?uri=" + encodeURIComponent(uri);
var likesUriDev = "/a/resource/likes?uri=" + encodeURIComponent(uri);
var sharesUriDev = "/a/resource/shares?uri=" + encodeURIComponent(uri);
req.http(likesUri).then(updateLikes).catch(error)
req.http(sharesUri).then(updateShares).catch(error)
var launcher = document.getElementById("launch-apollo")
launcher.addEventListener("click", function(){
var uri = actionsWrapper.getAttribute("data-uri")
var src = "https://www.apolloproject.xyz/a/resource?uri=" + encodeURIComponent(uri)
var srcDev = "http://localhost:8080/a/resource?uri=" + encodeURIComponent(uri)
var height = 575;
var width = 437
var top = (screen.height - height) / 4;
var left = (screen.width - width) / 2;
popup = window.open("", "ApolloResourceAction", 'top=100 left=' + left + ' width=437 height=575')
popup.document.write('<iframe width="' + width + '" height="' + height + '" allowTransparency="true" frameborder="0" scrolling="yes" style="width:100%;" src="'+ src +'" type="text/javascript"></iframe>');
})
function updateLikes(request){
var data = JSON.parse(request.responseText)
console.log(data)
likesSpan.innerHTML = data.likes
}
function updateShares(request){
var data = JSON.parse(request.responseText)
sharesSpan.innerHTML = data.shares
}
function addActionStyles(){
innerActionsWrapper.style.cssText = "position:relative;width:140px; height:57px;"
launchApollo.style.cssText = "color:#2cafed;font-size:39px;font-weight:bold;text-decoration:none;"
likesWrapper.style.cssText = "position:absolute; left:49px; top:0px; font-size:12px;"
sharesWrapper.style.cssText = "position:absolute; left:49px; bottom:27px; font-size:12px;"
likesSpan.style.cssText = "font-size:12px;"
sharesSpan.style.cssText = "font-size:12px;"
apolloSpan.style.cssText = "font-size:11px;display:block;clear:both;"
var css = '#launch-apollo:hover{ color: #617078 !important }' +
'#apollo-actions-wrapper{ text-align:left; display:inline-block; width:140px; height:57px; }';
var style = document.createElement('style');
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
document.getElementsByTagName('head')[0].appendChild(style);
}
var timer = setInterval(function () {
if (popup.closed) {
clearInterval(timer);
window.location.reload(); // Refresh the parent page
}
}, 1000);
function error(){
console.log(error)
}
Thank you.
It turned out Chrome had updated it's cookie requirements a while back for popups within external sites. In my case I had to set SameSite to None and Secure to true.
I'm currently on an Angular project (I'm a begineer) and I created a function to create a HTML form depending on the content of a JSON (the form then allows me to push new data in the JSON). It works, displays the form, but I didn't think ahead, and don't know how to retrieve the input values from the user.
createNewLineForm() {
if (this.allowForm) {
let indiceList = 0;
this.formOn = true;
for (let jsonCol of this.columnData) {
if (jsonCol.visible === true && jsonCol.type === "Boolean") {
var content = document.getElementById("formcontent");
var listTitle = document.createElement("header");
listTitle.textContent = jsonCol.name;
content.appendChild(listTitle);
var selectList = document.createElement("select");
selectList.id = "trueFalse";
content.appendChild(selectList);
var option = document.createElement("option");
option.value = "true";
option.text = "true";
selectList.appendChild(option);
var option2 = document.createElement("option");
option2.value = "false";
option2.text = "false";
selectList.appendChild(option2)
}
if (jsonCol.visible === true && jsonCol.type === "String" && jsonCol.dropdownList === null) {
var content = document.getElementById("formcontent");
var inputTitle = document.createElement("header");
inputTitle.textContent = jsonCol.name;
content.appendChild(inputTitle);
var formInput = document.createElement("input");
formInput.type = "text";
formInput.placeholder = jsonCol.name;
formInput.name = "text";
content.appendChild(formInput);
}
if (jsonCol.visible === true && jsonCol.type === "String" && jsonCol.dropdownList != null) {
var content = document.getElementById("formcontent");
var listTitle = document.createElement("header");
listTitle.textContent = jsonCol.name;
content.appendChild(listTitle);
var selectListLong = document.createElement("select");
selectListLong.name ="longList";
indiceList++;
content.appendChild(selectListLong);
for (const options of jsonCol.dropdownList) {
var option = document.createElement("option");
option.value = options;
option.text = options;
selectListLong.appendChild(option);
}
}
}
var submit = document.createElement("input");
submit.type = "submit";
content.appendChild(submit);
}
}
It's then conditionnally called in my HTML window with a simple <div id="formcontent"> </div>
I tried using document.getElementByName('...').value, doesn't seem to work.
Btw, if you have another idea on how to make that form, I'd like hearing it as well, this method seems a bit messy I think.
var content = document.getElementById("formcontent").value;
You can try it by ID
I am uploading an image and i expect base64 data of that image.I am trying below code that works to get image data as base64.
1) function getImage(file,count) //file is files[i] of input:file passed from for()
{
if(file)
{
var reader = new FileReader();
reader.onload = function(event)
{
var data = event.target.result;
var image = document.createElement("img");
image.id = 'img'+count;
image.class = 'edit';
image.src = data;
console.log('width:'+this.width);//not working value is undefined
console.log('height:'+this.height);//not working value is undefined
//image.title = 'click to edit';
image.data = count;
data = image.dataset;
data.ref=count;
document.getElementById('image'+count).appendChild(image);
jQuery(document.getElementById('image'+count)).removeClass('disable');
jQuery(document.getElementById('img'+count)).addClass('action editable img ');
}
reader.readAsDataURL(file);
}
}
This works fine and it creates an img tag with image loaded correctly.
But I want to know the image width and height before creating img element, so i tried below code,
2) function getImage(file,count)
{
if(file)
{
var reader = new FileReader();
reader.onload = function(event)
{
var img = new Image();
var data = event.target.result;
img.onload = function(){
console.log(img.width);
var image = document.createElement("img");
image.id = 'img'+count;
image.class = 'edit';
image.src = data;
console.log('width:'+this.width);
console.log('height:'+this.height);
image.data = count;
data = image.dataset;
data.ref=count;
document.getElementById('image'+count).appendChild(image);
jQuery(document.getElementById('image'+count)).removeClass('disable');
jQuery(document.getElementById('img'+count)).addClass('action editable img ');
};
}
reader.readAsDataURL(file);
}
}
But the second code does not prints log and img tag is not created. so what is the problem?What wrong in my code?Any better way to achieve this?.
just single line makes my problem null,
Thanks to this.
function getImage(file,count)
{
if(file)
{
var reader = new FileReader();
reader.onload = function(event)
{
var img = new Image();
var data = event.target.result;
img.src = data; //this line solved my problem..
img.onload = function(){
var image = document.createElement("img");
image.id = 'img'+count;
image.class = 'edit';
image.src = data;
console.log('width:'+this.width);
console.log('height:'+this.height);
image.data = count;
data = image.dataset;
data.ref=count;
document.getElementById('image'+count).appendChild(image);
jQuery(document.getElementById('image'+count)).removeClass('disable');
jQuery(document.getElementById('img'+count)).addClass('action editable img ');
};
}
reader.readAsDataURL(file);
}
}
Waiting for any better answer..
I've been have a heck of a time doing all this. I hope everyone can help.So... what this is doing is taking 9 text box number and adding them up in a dynamic text box. So here are my problems.
How can I replace an empty text box with a 0, if the user gets rid of the 0 that is already in there its will come out NaN. The if statements below were supposed to fix it, maybe someone can improve it.
stage.addEventListener(Event.CHANGE, checkTotal);
nextQuestion_btn.addEventListener(MouseEvent.MOUSE_DOWN, nextQuestion);
function checkTotal(e:Event){
var work:Number = parseInt(work_txt.text);
var rnr:Number = parseInt(rnr_txt.text);
var exerciseB:Number = parseInt(exerciseB_txt.text);
var exerciseM:Number = parseInt(exerciseM_txt.text);
var chores:Number = parseInt(chores_txt.text);
var social:Number = parseInt(social_txt.text);
var food:Number = parseInt(food_txt.text);
var twt:Number = parseInt(twt_txt.text);
var partying:Number = parseInt(partying_txt.text);
var other:Number = parseInt(other_txt.text);
if(work_txt.text==""){
work=0;
}
if(rnr_txt.text==""){
rnr=0;
}
if(exerciseB_txt.text==""){
exerciseB=0;
}
if(exerciseM_txt.text==""){
exerciseM=0;
}
if(chores_txt.text==""){
chores=0;
}
if(social_txt.text==""){
social=0;
}
if(food_txt.text==""){
food=0;
}
if(twt_txt.text==""){
twt=0;
}
if(partying_txt.text==""){
partying=0;
}
if(other_txt.text==""){
other=0;
}
var total400:Number = work + rnr + exerciseB + exerciseM +
chores + social + food + twt + partying + other;
I can't let my text boxes add up over 400 so as the user types in 399 into one box, if the user types 2 into the next that current text box will revert to 0 because it would be over 400.
I was told using e.currentTarget could solve that problem but I'm not sure how to use it.
All my code...This is my first time on this site so please forgive me for my noobness.
work_txt.maxChars = 3;
rnr_txt.maxChars = 3;
exerciseB_txt.maxChars = 3;
exerciseM_txt.maxChars = 3;
chores_txt.maxChars = 3;
social_txt.maxChars = 3;
food_txt.maxChars = 3;
twt_txt.maxChars = 3;
partying_txt.maxChars = 3;
other_txt.maxChars = 3;
work_txt.restrict = "0-9"
rnr_txt.restrict = "0-9"
exerciseB_txt.restrict = "0-9"
exerciseM_txt.restrict = "0-9"
chores_txt.restrict = "0-9"
social_txt.restrict = "0-9"
food_txt.restrict = "0-9"
twt_txt.restrict = "0-9"
partying_txt.restrict = "0-9"
other_txt.restrict = "0-9";
/*work_txt.text = "0";
rnr_txt.text = "0";
exerciseB_txt.text = "0";
exerciseM_txt.text = "0";
chores_txt.text = "0";
social_txt.text = "0";
food_txt.text = "0";
twt_txt.text = "0";
partying_txt.text = "0";
other_txt.text = "0";*/
var survival:Number = 0;
nextQuestion_btn.visible=false;
stage.addEventListener(Event.CHANGE, checkTotal);
nextQuestion_btn.addEventListener(MouseEvent.MOUSE_DOWN, nextQuestion);
function checkTotal(e:Event){
var work:Number = parseInt(work_txt.text);
var rnr:Number = parseInt(rnr_txt.text);
var exerciseB:Number = parseInt(exerciseB_txt.text);
var exerciseM:Number = parseInt(exerciseM_txt.text);
var chores:Number = parseInt(chores_txt.text);
var social:Number = parseInt(social_txt.text);
var food:Number = parseInt(food_txt.text);
var twt:Number = parseInt(twt_txt.text);
var partying:Number = parseInt(partying_txt.text);
var other:Number = parseInt(other_txt.text);
if(work_txt.text==""){
work=0;
}
if(rnr_txt.text==""){
rnr=0;
}
if(exerciseB_txt.text==""){
exerciseB=0;
}
if(exerciseM_txt.text==""){
exerciseM=0;
}
if(chores_txt.text==""){
chores=0;
}
if(social_txt.text==""){
social=0;
}
if(food_txt.text==""){
food=0;
}
if(twt_txt.text==""){
twt=0;
}
if(partying_txt.text==""){
partying=0;
}
if(other_txt.text==""){
other=0;
}
var total400:Number = work + rnr + exerciseB + exerciseM +
chores + social + food + twt + partying + other;
trace(work);
trace(rnr);
trace(exerciseB);
trace(exerciseM);
trace(chores);
trace(social);
trace(food);
trace(twt);
trace(partying);
trace(other);
trace(total400);
total400_txt.text = String(total400);
if(total400 >= 400){
nextQuestion_btn.visible=true;
}else{
nextQuestion_btn.visible=false;
}
}
Q1
If the values only can be int type, try to use int instead of Number
var work:int = parseInt(work_txt.text);//work will be 0 if the text is empty
Q2
If you want revert the text to 0(the text box which input 2)
function checkTotal(e:Event){
var target:TextField = e.target as TextField;
if(total400 >= 400){
if (target) {//you may check if target is one of the text box you have listed.
target.text = "0";
}
nextQuestion_btn.visible=true;
}
}
I'm working on a webpage for a class and am trying to work the navigation using ActionScript 3. My understanding of how it should work is that when you click the links at the top, it removes whatever is currently displayed, using removeChild, and adds the new page's content using addChild.
It works fine when going from the initial page to one of the links. But whenever I go from a page that is not the home page to another page I get:
ArgumentError: Error #2025: The supplied DisplayObject must be a child
of the caller. at flash.display::DisplayObjectContainer/removeChild()
at nutshellMathReCoded4_fla::MainTimeline/hideOtherPages() at
nutshellMathReCoded4_fla::MainTimeline/runSubtraction()
Here is the code:
stop();
// Flash Imports
import flash.events.MouseEvent;
import flash.events.Event;
import flash.media.SoundMixer;
//Page Components //
// Home //
// Links
// Create Header & Nav //
// Logo Link
var headerLogo:logoMainPage = new logoMainPage();
headerLogo.x = 252.85;
headerLogo.y = 18.00;
//Addition Link
var additionLink:addPageLink = new addPageLink();
additionLink.x = 142.35;
additionLink.y = 96.00;
//Subtraction Link
var subtractionLink:subPageLink = new subPageLink();
subtractionLink.x = 208.60;
subtractionLink.y = 144.60;
// Multiplication Link
var multiplicationLink:mulPageLink = new mulPageLink();
multiplicationLink.x = 397.80;
multiplicationLink.y = 100.00;
// Division Link
var divisionLink:divPageLink = new divPageLink();
divisionLink.x = 597.95;
divisionLink.y = 144.60;
//Animation
var animation:mainAnimation = new mainAnimation();
animation.x = 99.90;
animation.y = 244.00;
//Addition //
var addSimpleX:additionSimpleX = new additionSimpleX();
addSimpleX.x = 41;
addSimpleX.y = 395.95;
var addSimpleY:additionSimpleY = new additionSimpleY();
addSimpleY.x = 364;
addSimpleY.y = 395.95;
var addSimpleAnswer:additionSimpleAnswer = new additionSimpleAnswer();
addSimpleAnswer.x = 701;
addSimpleAnswer.y = 395.95;
var additionAddSign:additionSign = new additionSign();
additionAddSign.x = 276.55;
additionAddSign.y = 360.20;
var additionEqualsSign:equalsSign = new equalsSign();
additionEqualsSign.x = 613.95;
additionEqualsSign.y = 360.20;
var addSum:additionSummary = new additionSummary();
addSum.x = 178.25;
addSum.y = 202;
var addTryOut:tryItOut = new tryItOut();
addTryOut.x = 205.70;
addTryOut.y = 475.65;
var addDDLink:digDeeperLink = new digDeeperLink();
addDDLink.x = 274.95;
addDDLink.y = 696;
var addTQLink:quizLink = new quizLink();
addTQLink.x = 465;
addTQLink.y = 696.50;
//Addition Deeper
var addLong:additionExtended = new additionExtended();
addLong.x = 193.00;
addLong.y = 323.00;
var addDDSum:additionDDSummary = new additionDDSummary();
addDDSum.x = 222.40;
addDDSum.y = 201.20;
var addDDB2B:backToBasicsLink = new backToBasicsLink();
addDDB2B.x = 271.95;
addDDB2B.y = 696;
var addDDTQ:quizLink = new quizLink();
addDDTQ.x = 468;
addDDTQ.y = 696;
//Subtraction
var subSimpleX:subtractionSimpleX = new subtractionSimpleX();
subSimpleX.x = 44;
subSimpleX.y = 408.50;
var subSimpleY:subtractionSimpleY = new subtractionSimpleY();
subSimpleY.x = 367;
subSimpleY.y = 408.50;
var subSimpleAnswer:subtractionSimpleAnswer = new subtractionSimpleAnswer();
subSimpleAnswer.x = 692.95;
subSimpleAnswer.y = 408.50;
var subtractionEqualsSign:equalsSign = new equalsSign();
subtractionEqualsSign.x = 612.80;
subtractionEqualsSign.y = 372.95
var subtractionSubSign:subtractionSign = new subtractionSign();
subtractionSubSign.x = 280.85;
subtractionSubSign.y = 371.95;
var subTryOut:tryItOut = new tryItOut();
subTryOut.x = 221.8;
subTryOut.y = 477.95;
var subDDLink:digDeeperLink = new digDeeperLink();
subDDLink.x = 274.95;
subDDLink.y = 696;
var subTQLink:quizLink = new quizLink();
subTQLink.x = 465;
subTQLink.y = 696.50;
var subSum:subtractionSummary = new subtractionSummary();
subSum.x = 147.30;
subSum.y = 210.05;
// Subtraction Dig Deeper
var subBudget:budget = new budget();
subBudget.x = 367.95;
subBudget.y = 400.90;
var subExpenses:expenses = new expenses();
subExpenses.x = 204.00;
subExpenses.y = 513.40;
var subDDT1:subtractionDD1 = new subtractionDD1();
subDDT1.x = 264.05;
subDDT1.y = 211;
var subDDT2:subtractionDD2 = new subtractionDD2();
subDDT2.x = 206.65;
subDDT2.y = 351;
var subDDT3:subtractionDD3 = new subtractionDD3();
subDDT3.x = 131.80;
subDDT3.y = 474.95;
var subDDT4:subtractionDD4 = new subtractionDD4();
subDDT4.x = 291.00;
subDDT4.y = 463;
var subCheckBudgetLink:checkBudgetLink = new checkBudgetLink();
subCheckBudgetLink.x = 437;
subCheckBudgetLink.y = 609.5;
var subB2BLink:backToBasicsLink = new backToBasicsLink();
subB2BLink.x = 271.95;
subB2BLink.y = 696;
var subDDTQLink:quizLink = new quizLink();
subDDTQLink.x = 468;
subDDTQLink.y = 696;
//Multiplication
var mulSimpleX:multiplicationSimpleX = new multiplicationSimpleX();
mulSimpleX.x = 30.85;
mulSimpleX.y = 395.35;
var mulSimpleY:multiplicationSimpleY = new multiplicationSimpleY();
mulSimpleY.x = 370.85;
mulSimpleY.y = 395.35;
var mulSimpleAnswer:multiplicationSimpleAnswer = new multiplicationSimpleAnswer();
mulSimpleAnswer.x = 691.90;
mulSimpleAnswer.y = 395.35;
var multiplicationMulSign:multiplicationSign = new multiplicationSign();
multiplicationMulSign.x = 284.60;
multiplicationMulSign.y = 365.95;
var mulEqualsSign:equalsSign = new equalsSign();
mulEqualsSign.x = 609.80;
mulEqualsSign.y = 356.95;
var mulSummary:multiplicationSummary = new multiplicationSummary();
mulSummary.x = 145.30;
mulSummary.y = 202.40;
var mulTryOut:tryItOut = new tryItOut();
mulTryOut.x = 218.80;
mulTryOut.y = 461.95;
var mulDDLink:digDeeperLink = new digDeeperLink();
mulDDLink.x = 274.95;
mulDDLink.y = 696;
var mulTQLink:quizLink = new quizLink();
mulTQLink.x = 465.00;
mulTQLink.y = 696;
// Multiplication Deeper
var mulDD1:multiplicationDD1 = new multiplicationDD1();
mulDD1.x = 241.15;
mulDD1.y = 209;
var mulDD2:multiplicationDD2 = new multiplicationDD2();
mulDD2.x = 297.05;
mulDD2.y = 350.40;
var mulDD3:multiplicationDD3 = new multiplicationDD3();
mulDD3.x = 316.75;
mulDD3.y = 393.65;
var mulDD4:multiplicationDD4 = new multiplicationDD4();
mulDD4.x = 297.55;
mulDD4.y = 436.90;
var mulB2BLink:backToBasicsLink = new backToBasicsLink();
mulB2BLink.x = 271.95;
mulB2BLink.y = 696;
var mulDDTQL:quizLink = new quizLink();
mulDDTQL.x = 468;
mulDDTQL.y = 696;
// Division
var divSimpleX:divisionSimpleX = new divisionSimpleX();
divSimpleX.x = 42;
divSimpleX.y = 397.5;
var divSimpleY:divisionSimpleY = new divisionSimpleY();
divSimpleY.x = 366.95;
divSimpleY.y = 397.50;
var divSimpleAnswer:divisionSimpleAnswer = new divisionSimpleAnswer();
divSimpleAnswer.x = 698.95;
divSimpleAnswer.y = 397.50;
var divisionDivSign:divisionSign = new divisionSign();
divisionDivSign.x = 277.80;
divisionDivSign.y = 360.95;
var divEqualsSign:equalsSign = new equalsSign();
divEqualsSign.x = 609.80;
divEqualsSign.y = 361.95;
var divSummary:divisionSummary = new divisionSummary();
divSummary.x = 112.50;
divSummary.y = 202.20;
var divTryOut:tryItOut = new tryItOut();
divTryOut.x = 218.80;
divTryOut.y = 466.95;
var divDDLink:digDeeperLink = new digDeeperLink();
divDDLink.x = 274.95;
divDDLink.y = 696;
var divTQLink:quizLink = new quizLink();
divTQLink.x = 274.95;
divTQLink.y = 696;
// Division Deeper
var divDD1:divisionDD1 = new divisionDD1();
divDD1.x = 239.60;
divDD1.y = 215.00;
var divDD2:divisionDD2 = new divisionDD2();
divDD2.x = 328.65;
divDD2.y = 336.70;
var divDD3:divisionDD3 = new divisionDD3();
divDD3.x = 308.95;
divDD3.y = 381.20;
var divB2BLink:backToBasicsLink = new backToBasicsLink();
divB2BLink.x = 271.95;
divB2BLink.y = 696;
var divDDTQLink:quizLink = new quizLink();
divDDTQLink.x = 468;
divDDTQLink.y = 696;
// Quiz
//Title
var titleQuiz:quizTitle = new quizTitle();
titleQuiz.x = 419.40;
titleQuiz.y = 192;
// Random Number Backgrounds
var quizBG1:shortTextBG = new shortTextBG();
quizBG1.x = 44.9;
quizBG1.y = 283;
var quizBG2:shortTextBG = new shortTextBG();
quizBG2.x = 368.95;
quizBG2.y = 283;
var quizBG3:shortTextBG = new shortTextBG();
quizBG3.x = 44.9;
quizBG3.y = 360.25;
var quizBG4:shortTextBG = new shortTextBG();
quizBG4.x = 368.95;
quizBG4.y = 360.25;
var quizBG5:shortTextBG = new shortTextBG();
quizBG5.x = 44.90;
quizBG5.y = 440.20;
var quizBG6:shortTextBG = new shortTextBG();
quizBG6.x = 368.95;
quizBG6.y = 440.20;
var quizBG7:shortTextBG = new shortTextBG();
quizBG7.x = 44.90;
quizBG7.y = 518.80;
var quizBG8:shortTextBG = new shortTextBG();
quizBG8.x = 368.95;
quizBG8.y = 518.80
// Signs
var quizAddSign:additionSign = new additionSign();
quizAddSign.x = 280.05;
quizAddSign.y = 251.90;
var quizSubSign:subtractionSign = new subtractionSign();
quizSubSign.x = 281.40;
quizSubSign.y = 323.80;
var quizMulSign:multiplicationSign = new multiplicationSign();
quizMulSign.x = 288.20;
quizMulSign.y = 414.05;
var quizDivSign:divisionSign = new divisionSign();
quizDivSign.x = 280.05;
quizDivSign.y = 489.20;
var quizAddEqualsSign:equalsSign = new equalsSign();
quizAddEqualsSign.x = 603.90;
quizAddEqualsSign.y = 251.90;
var quizSubEqualsSign:equalsSign = new equalsSign();
quizSubEqualsSign.x = 603.90;
quizSubEqualsSign.y = 330.55;
var quizMulEqualsSign:equalsSign = new equalsSign();
quizMulEqualsSign.x = 603.90;
quizMulEqualsSign.y = 409.20;
var quizDivEqualsSign:equalsSign = new equalsSign();
quizDivEqualsSign.x = 603.90;
quizDivEqualsSign.y = 489.20;
// Check quiz
var quizCheck:checkQuiz = new checkQuiz();
quizCheck.x = 369.95;
quizCheck.y = 626.60;
var answerWrong:wrongAnswer = new wrongAnswer();
var answerCorrect:correctAnswer = new correctAnswer();
// Home Page //;
// Create Header & Nav //
// Logo
addChild(headerLogo);
addChild(additionLink);
addChild(subtractionLink);
addChild(multiplicationLink);
addChild(divisionLink);
addChild(animation);
headerLogo.addEventListener(MouseEvent.CLICK, runIndexPage);
function runIndexPage(e:MouseEvent):void
{
// Hide other Pages
hideOtherPages();
addChild(animation);
animation.gotoAndPlay(1);
}
// Addition Page //
additionLink.addEventListener(MouseEvent.CLICK, runAddition);
function runAddition(e:MouseEvent):void
{
hideOtherPages();
createAdditionPage();
}
subtractionLink.addEventListener(MouseEvent.CLICK, runSubtraction);
function runSubtraction(e:MouseEvent):void
{
hideOtherPages();
createSubtractionPage();
}
multiplicationLink.addEventListener(MouseEvent.CLICK, runMultiplication);
function runMultiplication(e:MouseEvent):void
{
hideOtherPages();
createMultiplicationPage();
}
divisionLink.addEventListener(MouseEvent.CLICK, runDivision);
function runDivision(e:MouseEvent):void
{
hideOtherPages();
createDivisionPage();
}
//Global Functions
function createAdditionPage():void
{
addChild(addSimpleX);
addChild(addSimpleY);
addChild(addSimpleAnswer);
addChild(additionAddSign);
addChild(additionEqualsSign);
addChild(addSum);
addChild(addTryOut);
addChild(addDDLink);
addChild(addTQLink);
}
function createSubtractionPage():void
{
addChild(subSimpleX);
addChild(subSimpleY);
addChild(subSimpleAnswer);
addChild(subtractionSubSign);
addChild(subtractionEqualsSign);
addChild(subSum);
addChild(subTryOut);
addChild(subDDLink);
addChild(subTQLink);
}
function createMultiplicationPage():void
{
addChild(mulSimpleX);
addChild(mulSimpleY);
addChild(mulSimpleAnswer);
addChild(multiplicationMulSign);
addChild(mulEqualsSign);
addChild(mulSummary);
addChild(mulTryOut);
addChild(mulDDLink);
addChild(mulTQLink);
}
function createDivisionPage():void
{
addChild(divSimpleX);
addChild(divSimpleY);
addChild(divSimpleAnswer);
addChild(divisionDivSign);
addChild(divEqualsSign);
addChild(divSummary);
addChild(divTryOut);
addChild(divDDLink);
addChild(divTQLink);
}
function hideOtherPages():void
{
if(animation)
{
removeChild(animation);
SoundMixer.stopAll();
}
else if(addSimpleX)
{
removeChild(addSimpleX);
removeChild(addSimpleY);
removeChild(addSimpleAnswer);
removeChild(additionAddSign);
removeChild(additionEqualsSign);
removeChild(addSum);
removeChild(addTryOut);
removeChild(addDDLink);
removeChild(addTQLink);
}
else if(addLong)
{
removeChild(addLong);
removeChild(addDDSum);
removeChild(addDDB2B);
removeChild(addDDTQ);
}
else if(subSimpleX)
{
removeChild(subSimpleX);
removeChild(subSimpleY);
removeChild(subSimpleAnswer);
removeChild(subtractionSubSign);
removeChild(subtractionEqualsSign);
removeChild(subSum);
removeChild(subTryOut);
removeChild(subDDLink);
removeChild(subTQLink);
}
else if(subBudget)
{
removeChild(subBudget);
removeChild(subExpenses);
removeChild(subDDT1);
removeChild(subDDT2);
removeChild(subDDT3);
removeChild(subDDT4);
removeChild(subCheckBudgetLink);
removeChild(subB2BLink);
removeChild(subDDTQLink);
}
else if(mulSimpleX)
{
removeChild(mulSimpleX);
removeChild(mulSimpleY);
removeChild(mulSimpleAnswer);
removeChild(multiplicationMulSign);
removeChild(mulEqualsSign);
removeChild(mulSummary);
removeChild(mulDDLink);
removeChild(mulTQLink);
}
else if(mulDD1)
{
removeChild(mulDD1);
removeChild(mulDD2);
removeChild(mulDD3);
removeChild(mulDD4);
removeChild(mulB2BLink);
removeChild(mulDDTQL);
}
else if(divSimpleX)
{
removeChild(divSimpleX);
removeChild(divSimpleY);
removeChild(divSimpleAnswer);
removeChild(divisionDivSign);
removeChild(divEqualsSign);
removeChild(divSummary);
removeChild(divTryOut);
removeChild(divDDLink);
removeChild(divTQLink);
}
else if(divDD1)
{
removeChild(divDD1);
removeChild(divDD2);
removeChild(divDD3);
removeChild(divB2BLink);
removeChild(divDDTQLink);
}
else if(titleQuiz)
{
removeChild(titleQuiz);
removeChild(quizBG1);
removeChild(quizBG2);
removeChild(quizBG3);
removeChild(quizBG4);
removeChild(quizBG5);
removeChild(quizBG6);
removeChild(quizBG7);
removeChild(quizBG8);
removeChild(quizAddSign);
removeChild(quizSubSign);
removeChild(quizMulSign);
removeChild(quizDivSign);
removeChild(quizAddEqualsSign);
removeChild(quizSubEqualsSign);
removeChild(quizMulEqualsSign);
removeChild(quizDivEqualsSign);
removeChild(quizCheck);
//Dynamic Quiz Removal Section ****
}
}
This code looks terrifying, but I won't dive into it as it is not the merits of the question. The problem is that you are only checking (in function hideOtherPages()) an existence of the objects, and not if they are added to Stage. You cannot remove from stage an object that is not there, this is why you gets this error, and it states just that.
You can check if your objects are added to stage before you will try to remove them.
You can remove all children from an object by simple loop.
The second option is the best in this case. You would create a Sprite, and...
var myContainer:Sprite = new Sprite();
addChild(myContainer);
// Here you are adding children/first set of visible objects
myContainer.addChild(myObject);
// ...
And now just before you will add new set of objects to container you can call "clearContainer()" function to remove previous ones.
function clearContainer() : void {
while(myContainer.numChildren) {
myContainer.removeChildAt(0);
}
}
Your code will be GREATLY reduced because of this, as you won't be forced to remove every item manually...
Alternatively if you want to keep a brute force, you can just check if objects exist before remove them manually, and you can do this by using function getChildByName(name:String) . You can set a property "name" for your objects and then check if "getChildByName" will return object or null, and if it is an object, you would remove it. So at the beginning it would look like this:
var divisionLink:divPageLink = new divPageLink();
divisionLink.x = 597.95;
divisionLink.y = 144.60;
divisionLink.name = "divisionLink"; // setting a name
And your removing would look like this:
else if(divisionLink && this.getChildByName("divisionLink"))
{
removeChild(divisionLink);
}
And do this with every item. This is tedious and slow, so I wouldn't recommend it, but included it for educational purposes.