I'm looking to incorporate a random number into the window name in this code snippet:
var rand = 185656;
setTimeout("win+rand+2 = window.open('url'+secid , '_blank')",2000);
The +rand+ part is not functioning as expected.
I'm looking to incorporate a random number into the window name in this code snippet:
var rand = 185656;
setTimeout("win+rand+2 = window.open('url'+secid , '_blank')",2000);
The +rand+ part is not functioning as expected.
Instead of passing a string to the setTimeout
function, it's recommended to pass a function and explicitly set your global property on the window
object. This way, you can use square bracket notation:
setTimeout(function () {
window["win" + rand + "2"] = window.open("url" + secid, "_blank");
}, 2000);
The main reason for avoiding passing a string to setTimeout
(or setInterval
) is that it essentially uses eval
, which can pose security risks.
If you pass a string like in your current setup, it will result in a reference error. It's akin to attempting something like this, which clearly won't work:
"a" + "b" = "c"; // ReferenceError: Invalid left-hand side in assignment
The content should always be outside of the quotation marks.
setTimeout("win"+rand+"2 = window.open('url'+secid , '_blank')",2000);
To start, it's crucial to avoid passing strings to setTimeout and instead opt for using the callback function. Additionally, ensure that the variable is placed outside of the string (quotes) when concatenating:
let num = 587332;
setTimeout(function() {
window["popup" + num + "3"] = window.open('newurl'+uniqueid , '_blank');
}, 3000);
You must ensure to "open" and "close" the String properly:
setTimeout("win" + rand + "2 = window.open('url'" + secid + " , '_blank')",2000);
Refer to this helpful article for more details.
Avoid passing executable code to setTimeout
(since it will be eval
'd), rather use a function reference or an anonymous function.
Furthermore, creating variable names in this manner is not recommended. Consider utilizing objects and keys instead.
You might want to consider the following approach:
setTimeout(function(){
var rand = 185656;
window['win' + rand + '2'] = window.open('url'+secid , '_blank'); //adds a property to the global object
}, 2000);
Perhaps the issue lies with the quotation marks
setTimeout("win"+parseInt(rand)+"2 = window.open('url'+secid , '_blank')",2000);
After a brief delay of 2 seconds, a new window will open with a dynamically generated URL.
Utilizing JavaScript and jQuery, I am developing a handler for form submission that will either allow or prevent the form from being submitted based on certain conditions. Although this task is fairly straightforward, it becomes more complex due to the ne ...
I acquired a theme that includes html, css3, and js files, and I included the file path as shown below: <!-- Basic --> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Porto - Responsive HTML5 Te ...
Utilizing NodeJS tools within Visual Studio, I am in the process of building a SharePoint Framework (SPF) App following the steps outlined here. Despite completing all the required steps, I encountered this error: Severity Code Description Project ...
I'm having trouble finding a solution for what seems like a simple issue... Currently, my header has a border-bottom and I'd like to change the color of this border based on the section the user is in. For example, consider these parent pages: ...
Below is the code to fetch data from a CSV file in AWS S3 using Node.js backend. The challenge here is handling more than 200k records without consuming too much memory and efficiently returning it to the frontend. AWS.config.update({ accessKeyId: ...
UPDATE: I have now included the res.send(json) that was missing from the snippet. I'm currently working on a project that involves extracting/scraping data from a website and consolidating it into JSON format. However, when I call the endpoint, the r ...
I am working with a multidimensional array that looks like this: myArray = [ ["ooooh", "that"], ["dog ", "of"], ["mine", "word...."] ] My goal is to remove the array item: ["ooooh", "that"] Although I know that element 0 is "ooooh" and element 1 is ...
I am trying to implement a feature where a div is displayed on the onchange event of a dropdownlist using JavaScript, but I keep getting an error message saying object required Here is the ASPX code snippet: <asp:DropDownList runat="server" ID="lstFil ...
Currently, I am delving into learning AngularJS by working on a simple webpage for a company. This page will showcase products that are fetched from a json file. Following a tutorial from the AngularJS website, which covers the exact task I aim to achieve, ...
Currently, I am in the process of transitioning my create-react-app from JavaScript to TypeScript. To help with this migration, I followed the steps outlined on create-react-app.dev and successfully installed the necessary packages by running: npm install ...
I am currently working on a project that utilizes both react and node (express). When I include react using src="https://unpkg.com/react@16/umd/react.development.js", everything works fine with JSX in the project. However, when I attempt to import like thi ...
Need some assistance, hoping for your help. I have 2 choices with subcategories. 1 - Option A ---- variant 1 ---- variant 2 ---- variant 3 2 - Option B ---- variant 4 ---- variant 5 ---- variant 6 Check out my code here Users must choose betwe ...
I found this script online and have been experimenting with it. However, I encountered the following error: SyntaxError: JSON.parse: unexpected character [Break On This Error] var res = JSON.parse(result); The problem lies in the file below as I am unf ...
I am working on a page called pages/conversations/[id].tsx, and here is the code: import Router, { useRouter } from 'next/router' export default function ConversationPage() { const router = useRouter() ... return ( <View ...
I received an eslint error message after inserting // eslint-disable-next-line react-hooks/exhaustive-deps into my code. 8:14 error Rule 'react-hooks/exhaustive-deps' definition not found I tried to resolve this issue by referring to this p ...
I am currently working on parsing a Json object and comparing it to another array in order to identify matching elements. So far, I have successfully written the following code. var gpaGrades = '{"A": 4, "A-": 3.67, "B+": 3.33, "B": 3, "B-": 2.67, "C ...
In my current project, I've implemented a custom micro-library similar to JQuery, but tailored specifically to my needs. window.$ = function(selector, context, undefined) { var getSelectorTest = function(selector, context, undefined) { ...
I need to sort users based on their data attribute. Within the main div called user-append, I have a variable number of users retrieved from an ajax get request. It could be 3 users or even up to 100, it's dynamic. Here is the structure with one user ...
As a beginner in python programming, I was following a project tutorial on YouTube and encountered a roadblock with a specific part of the code. You can find the complete code here: GitHub link and the segment in the YouTube video is at 24:30 - YouTube li ...
For my project in Angular 1.4, I am utilizing the ngOptions directive to dynamically fill a <select> element with <option> elements based on an object structure like this: {black: '#000000', red: '#FF0000', ...} The implem ...