surveyjs / survey-library

Free JavaScript form builder library with integration for React, Angular, Vue, jQuery, and Knockout.
https://surveyjs.io/form-library
MIT License
4.09k stars 795 forks source link

visibleif will display based on current url #6430

Open LiyanaKayor92 opened 1 year ago

LiyanaKayor92 commented 1 year ago

Are you requesting a feature, reporting a bug or asking a question?

hi, I new in vue and surveyjs. I want to ask, how i can display some question based on url paste when using visibleif. i have 1 form survey, in one form there have multiple pages, for first page, i have 2 question, the question will only display 1 based on current url. For example when i go to this link http://localhost:8081/#/survey-creator?survey=q1, the question 1 will appear and question 2 will hide.

here my code

function getSurvey() { var currentUrl = window.location.href; if(currentUrl.includes("q1")){ return "http://localhost:8081/#/survey-creator?survey=q1" }else if(currentUrl.includes("q2")){ return "http://localhost:8081/#/survey-creator?survey=q2" } }

const surveyJson = { pages: [ { elements: [ { name: "submit-form-1", title: "......", type: "radiogroup", choices: [ { value: 1, text: "Yes, I agree to participate" }, { value: 2, text: "No, I do not want to participate" } ], isRequired: true, visibleIf: getSurvey() == "http://localhost:8081/#/survey-creator?survey=q1", // here i want to use visibleif }, { name: "submit-form-2", title: "......" , type: "radiogroup", choices: [ { value: 1, text: "Yes, I agree to participate" }, { value: 2, text: "No, I do not want to participate" } ], isRequired: true, }, ] },

What is the current behavior?

What is the expected behavior?

How would you reproduce the current behavior (if this is a bug)?

Provide the test code and the tested page URL (if applicable)

Tested page URL:

Test code

your_code_here

Specify your

andrewtelnov commented 1 year ago

You can use variables. You can get a parameter from url set it's value into survey variable.

var url = new URL(window.location.href);
//get your survey parameter from url
var survey_parameter = url.searchParams.get("survey");
//Set variable
survey.setVariable("survey_parameter", survey_parameter);

After that you can use this variable as the following: visibleIf: "{survey_parameter} = 'q1'"

Thank you, Andrew