Abusing JavaScript string operations [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I've got this function in javascript:
g.changeImage = function (a) {
h.src = "image" + a + ".png";
};
It's used like this:
g.changeImage(2);
And it changes image h. The problem is, that it cannot use image from other directory than current.
I'm writing "addon" for the website which should hook into the existing script and change few things. One of them require changing h.src. Is there any way I can fool the browser and change the address of h to custom url using only the function given?
I can't access h directly, nor change existing script on the website. I can only use function given.
Thank you for any help.

One option that I can come up with off the top of my head would be to overwrite the function:
Old Function:
g.changeImage = function (a) {
h.src = "image" + a + ".png";
alert(h.src);
};
g.changeImage("test.png")
New Function:
g.changeImage = function (a) {
h.src = "test - new url " + a + ".png";
alert(h.src);
}
g.changeImage("test")
Here's a quick example for you to play with and see if this will work for what you need: http://jsfiddle.net/2BsQP/

Related

Shorthand way of creating html elements from an array of arrays [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Is there a way to make this into something shorter? I have a list of 15 questions and need them to stay in order. after each question is answered it go to a nextQuestion function. I would like to keep that as is. I've tried doing a for loop, but it keeps making the questions come out of order. Also, I'm very new to this. Any help would be greatly appreciated!
function questionContent() {
$("#gameShow").append("<p><strong>" +
questions[questionTracker].question +
"</p><p class='choices'>" +
questions[questionTracker].choices[0] +
"</p><p class='choices'>" +
questions[questionTracker].choices[1] +
"</p><p class='choices'>" +
questions[questionTracker].choices[2] +
"</p><p class='choices'>" +
questions[questionTracker].choices[3] +
"</strong></p>");
}
Not clear to me how your array of questions is put together, but your function looks like it is intended to take a single question and create the html for that question along with an array of answer choices.
To simplify the function in your question, you can handle the first question property and then loop through the choices array to add the rest of the html you are looking to append. Something like the example below (left out your strong tags cause they seem oddly placed and I wasn't sure where you really wanted them).
You could loop through your main questions array and call this function (or just create a single function to do it all) but hard to say exactly how you should handle that without more code or data in your example.
function questionContent(questions) {
let question = `<p>${questions[questionTracker].question}</p>`;
for (const choice of questions[questionTracker].choices) {
question += `<p class="choices">${choice}</p>`;
}
$("#gameShow").append(question);
}

How to read filename, strip out the name and others, create a folder and move the file to it folder with nodejs [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I will like some assistant on this.
i want to create a script that will read/watch a folder.
pull out the filename
move the file to its folder and organize it properly.
Example:
i have files in this order
../downloads/in.the.moment.s01e05.480p.mkv
../downloads/in.the.moment.s01e04.480p/in.the.moment.s01e04.480p.mkv
../downloads/boma.s04e04.mkv
../downloads/boma.s04e06/boma.s04e06.mkv
../downloads/things.falls.out.1080p/things.falls.out.1080p.mkv
and i want it to move and arrange the like this.
../series/in.the.moment/s01/in.the.moment.s01e04.480p.mkv
../series/in.the.moment/s01/in.the.moment.s01e05.480p.mkv
../series/boma/s04/boma.s04e04.mkv
../series/boma/s04/boma.s04e06.mkv
../movies/things.falls.out.1080p.mkv
Well reading and writing files is pretty easy; you can learn how to do that on your own. As for creating a new route based on what you have there, you can just map over the files one by one like so:
function mapToNewPath(filename) {
// this regexp strips out different parts of the filename
// first capture group is for series name
// second capture group is for the season
// only matches series episodes
var matches = filename.match(/^(.*?)\.(s[0-9]*)e[0-9]*/);
var series = matches && matches[1];
var season = matches && matches[2];
if (series) {
return 'series/' + series + '/' + season + '/' + filename;
}
return 'movies/' + filename;
}

JS - document.createElement does not work [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have the following code:
function icon(link) {
var iccon = document.createElement('div');
var iccons = document.createElement('td');
iccon.setAttribute('id', 'icon');
icons.appendChild(iccons);
iccon.setAttribute('onclick', 'window.open("' + link + '");');
iccons.appendChild(iccon);
var icons = document.getElementById('icons');
};
The HTML code is here.
Even though it's hard to say what you're trying to achieve with the code, I must say that it's an ordering error: you want to append the newly created items to the #icons element, which you declare at the very end.
Move the last statement to the top of the function and everything will work as expected:
function icon(link) {
var icons = document.getElementById('icons');
var iccon = document.createElement('div');
var iccons = document.createElement('td');
iccon.setAttribute('id', 'icon');
icons.appendChild(iccons);
iccon.setAttribute('onclick', 'window.open("' + link + '");');
iccons.appendChild(iccon);
};
Here's the updated demo: http://codepen.io/gion/pen/NRmgPJ
I must say that you should be more careful about naming. Things get hard to read when they aren't named well.

Javascript parse a page for find hex color values [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
in my project i need a function for find all hex color values into a webpage (like #FFDD600, #999, etc).
In the past to do the same thing I used php class "PHP Simple HTML DOM Parser", but now I need that # research is carried out ​​in javascript or jquery.
For a study I need to catalog some websites registering the colors used.
Any idea?
Thanks in advance
This will fetch an url and parse it as text for all hex values, and put them into current page body.
You can check it out here: JSFiddle
Though it will be somewhat limited due to cross-origin policy.
$.ajax({
url: ENTER_URL_HERE,
success: function (data) {
var hexCodes = $("html").html().match(/#[a-fA-F0-9]{6}|#[a-fA-F0-9]{3}/g);
$("body").html("");
$.each(hexCodes, function () {
$("body").append("<p>" + this + "</p>");
});
}
});

How to "parse" js twice? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
The following Code should output:
txt1txt2txt3...
var result ='';
var help='';
var a_1 = "txt1";
var a_2 = "txt2";
...
for (i=1;i<=100;i++)
{
help = 'a_' + i; // This line should be "parsed twice" !
result = result + help;
document.write(result);
}
Of course, I can avoid this by using the if-, or case-function for each "a_ i",
but this would make my js file very big.
So is there a way to parse this "help line" twice with js?
I asume, I could do it with including one js file in another, but I dont like this way?
No PHP , because the code should work clientside
Better to use an array.
eval() is the function for "double parsing" (but could be really dangerous !)

Categories