I need to call this from the code behind if there is a certain value read in from a
query string variable ie
x as string = request.querysting("Var")
if x = 'Hide" then
function guestHide()
{
$(".panel").hide("slow");
}
end if
in your aspx page do this
<%
if (Request.QueryString["var"].ToString() == "Hide") {
%>
function guestHide() { $(".panel").hide("slow"); }
<%
}
%>
The above is C#, it should be easily translatable to VB.
Related
I am trying to build a 4-choice quiz application using Ruby on Rails and JavaScript.
If you click one of the Choice Buttons at the Question View, a helper method "updateData(bool)" updates database and then you'll see the result without view transition. And if you click the Chart Button after answering, the view transitions to the Chart View.
However, the helper method "updateData" seems to be called not only by Choice Button but by Chart Button. It updates the database and increment the Question Number against my expectation.
According to the outputs from console.log, it is not likely that the Chart Button calls JavaScript function sendAnswer(ans).
I use Rails 5.0.1.
I would appreciate any advice. Thanks!
<head>
<script>
function sendAnswer(ans){
var questionID = getNumber();
var data = readCSV(questionID);
// True if answer number matches correct answer number
if(data[5] == ans ){
console.log("true")
<% updateData(1) %>
}else{
console.log("false")
<% updateData(0) %>
};
// Create Chart Button after answering
$("#bottom").append('<div id="showChart"></div>');
$("#showChart").html('<button id="showChartBtn" onclick="showChart()">Show Chart</button>');
};
</script>
<script>
function showChart(){
window.location.href = "/chart";
};
</script>
</head>
<body>
<% alphabets = ["A", "B", "C", "D"] %>
<!-- For loop to make A-D choices -->
<% for i in 0..3 %>
<button class="choice_btn" id="btn_<%= i %>" onclick="sendAnswer(<%= i %>);">
<%= alphabets[i] %>
</button>
<% end %>
<div id="bottom">
<!-- Chart Button comes here -->
</div>
</body>
# Return user according to remember token
def current_user
if (user_id = session[:user_id])
#current_user ||= User.find_by(id: user_id)
elsif (user_id = cookies.signed[:user_id])
user = User.find_by(id: user_id)
if user && user.authenticated?(:remember, cookies[:remember_token])
log_in user
#current_user = user
end
end
end
# Increment the Question Number, and increment the Correct Answer Number if true
def updateData(bool)
current_user.questionNum += 1
current_user.correctNum += bool
current_user.save
end
In the html code above, embedded Ruby <% updateData %> is executed while generating HTML.
I should instead call Controller Action by Button Click Event, and then call helper method via Action.
I want to store mysql strings in a javascript array variable. I am using jsp for server-side.
I tried it in three ways. All the three aren't working. Need some help.
Attempt-1:
<script>
var name = [];
<%
st=con.prepareStatement("select name from company");
rs=st.executeQuery();
while(rs.next()){
String s = rs.getString(1);
%>
name.push(<%=s%>);
<%
}
%>
</script>
Attempt-2:
<script>
var name = [];
<%
st=con.prepareStatement("select name from company");
rs=st.executeQuery();
while(rs.next()){
%>
name.push(<%=rs.getString(1)%>);
<%
}
%>
</script>
Attempt-3:
<script>
var name = [];
<%
st=con.prepareStatement("select name from company");
rs=st.executeQuery();
while(rs.next()){
%>
name.push(<%out.print(rs.getString(1));%>);
<%
}
%>
</script>
All the three attempts showed the same result and error after processing.
Interpreted Code:
<script>
var name = [];
name.push(tcs);
name.push(wipro);
</script>
Error:
ReferenceError: tcs is not defined
You should add " around your string also you should javascript encode it(I think apache has a library that will do this for you):
<script>
var name = [];
<%
st=con.prepareStatement("select name from company");
rs=st.executeQuery();
while(rs.next()){
String s = StringEscapeUtils.escapeJavaScript(rs.getString(1));
%>
name.push("<%= s %>");
<%
}
%>
</script>
Try using this Apache library to escape the java string based on javascript rules: StringEscapeUtils
Javascript is trying to interpret name.push(tcs); tcs as a variable which in this case tcs has not been declared or initialized it is undefined. Instead you want javascript to interpret tcs as a string so you need quotes around it "tcs" or in your case "<%= s %>".
I have a need where in my application, I need to copy the URL that is rendered to the user. I am using the JS from this JSFiddle.
My code is slightly different from the HTML part of the Fiddle. I use mscConfirm for the JS message box.
My show.js.erb file,
<% if #exists == true %>
mscConfirm("Hold on", 'The file has been shared already, <%= #display %>', function(){
clipboard(this);
});
<% end %>
The function in the above code, is executed on clicking OK to the Message that appears. On doing Ctrl + C to the #display value in the screen, I still get 'undefined' when I try to paste it elsewhere.
My textfield output after pasting,
Any help is appreciated. Thanks in advance.
The provided clipboard() function accepts a DOM element. You will have to modify the function to suit your requirements.
function clipboard(el, txt) {
// deselect all
var selected = document.getElementsByClassName("selected");
for (var i = 0; i < selected.length; i++) {
selected[i].className = '';
};
el.className = 'selected';
clip.setValue(txt);
}
Here, you will have to explicitly pass the text to be copied. In mscConfirm's okCallback, this does not refer to any element.
So according to me, your function call should look like this:
<% if #exists == true %>
mscConfirm("Hold on", 'The file has been shared already, <%= #display %>', function(){
clipboard(document.getElementById("DOMnodeID"), #display);
});
<% end %>
Or you can remove the DOMnode altogether and just pass the text to be copied:
function clipboard(txt) {
// deselect all
var selected = document.getElementsByClassName("selected");
for (var i = 0; i < selected.length; i++) {
selected[i].className = '';
};
clip.setValue(txt);
}
Then your function call should look like this:
<% if #exists == true %>
mscConfirm("Hold on", 'The file has been shared already, <%= #display %>', function(){
clipboard(#display);
});
<% end %>
I have a method inside helper which fires an action which changes value of instance variable. Inside my javascript I've done this:
if(cat_id == 1)
{
<% cat_appointments(1) %>
}
else if(cat_id == 2)
{
<% cat_appointments(2) %>
}
else if(cat_id == 3)
{
<% cat_appointments(3) %>
}
My helper:
module StaticHelper
def cat_appointments(cat_id)
#appointments = Appointment.where(cat_id: cat_id)
end
end
The problems is that appointments are set like every time there is selected cat with id=3. If I put alert in those conditions, alert with selected id is displayed correctly when user selects specific cat.
Any ideas what is wrong with this code? How to change that variable?
When I replace:
else if(cat_id == 2)
{
<% cat_appointments(2) %>
}
with:
else if(cat_id == 3)
{
<% cat_appointments(3) %>
}
Then there are not always displayed appointments with cat_id=3. Now there are always displayed events with cat_id=2. So, the last 'else if' statement is always correct.
Thank you.
Can you try adding =
<%= cat_appointments(1) %>
I'm interested in using the jQuery UI autocomplete plugin in my Rails app. The number of possible values will be small, so I wanted to store them client-side. So I have my controller set up as follows:
def index
#tags = Tag.find(:all).map { |t| t.name }
end
And in my view:
var tags = <%= #tags %>
The problem is that this renders as:
var tags = ["tag1","tag2"];
Instead of:
var tags = ["tag1","tag2"]
What do I need to do differently in order to stop escaping those quotes inside my tag array?
What about:
var tags = <%=raw #tags %>;
EDIT:
in your controller
#tags = Tag.find(:all).map(&:name).to_json
in your view:
var tags = <%= #tags %>;
It's the same as what you presented but you're sure it's valid and sure (because escaped) json (+ the query is improved).
I'm still questioning about the XSS + raw...
I was able to solve this by changing my view code to:
<%= array_or_string_for_javascript(#tags) %>
The thing missing in your controller is html_safe - here is an example:
controller
#keys = #categories.map { |x| x.name }
#autocomplete_categories = #keys.to_json.html_safe
view:
<script type="text/javascript">
$(document).ready(function() {
var data = <%= #autocomplete_categories %>;
$("#auto").autocomplete( { source: data } );
});
</script>